Click here to Skip to main content
15,912,493 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to select one row from mysql using limit 0,1 and update one column from selected row

eg:

update ( select * from billgeneration limit 0,1 ) as d set TotalAmount = 2000
Posted
Updated 17-Feb-13 23:22pm
v2
Comments
Bernhard Hiller 18-Feb-13 5:12am    
Do you have to do that in MySQL directly? Or do you access the database from an application written in VB.Net, C#, Java, ...?

First of all you fetch total information in one DataSet (or) DataTable.
After that you can use foreach loop , fetch the each and every row information. Inside the foreach you write you update query. this is in application wise...

ex:

C#
DataTable dt=//total data;

foreach(DataRow dr in dt.Rows)
{
    //your update query
}



In DB itself also you can do this

Ex:

C#
DataSet ds= //data;
string data =ds.GetXML();


In your SQL SP itself you can follow the below steps to achieve this
Fetch the XML path information and store it in one temprory table. Then,

SQL
DECLARE @MINCOUNT INT , @MAXCOUNT INT        
SELECT @MAXCOUNT = COUNT(*) FROM #TEMPTable        
SET @MINCOUNT = 1    

  
DECLARE @Update_Count VARCHAR(10)  
SET @Update_Count=0   

While( @MINCOUNT<= @MAXCOUNT) 
BEGIN
   //UPDATE QUERY
   //SET @Update_Count=@Update_Count+1
SET  @MINCOUNT= @MINCOUNT+1
END


THIS is DB side....
 
Share this answer
 
v2
Comments
robinsonpaul 18-Feb-13 5:39am    
how to create temp table in mysql
Naveen.Sanagasetti 18-Feb-13 22:49pm    
Select col1,col2,col3
into #TEMPTable
from table1

this is the sample syntax only ..
SQL
update ( select * from billgeneration limit 0,1 ) as d set TotalAmount = 2000

It is not possible in mysql. However you have to select first then iterate over the result and then fire the update on each itaration. Procedure or function is the good choice to do this.
 
Share this answer
 
Hi,

you can do like direct Update as

SQL
update billgeneration set TotalAmount = 2000 WHERE limit IN(0,1)


Check the following link
UPDATE Syntax[^]

Regards,
GVPrabu
 
Share this answer
 
Comments
Naveen.Sanagasetti 18-Feb-13 6:18am    
If they want to update more than one row how to achieve using this query..?
gvprabu 18-Feb-13 6:42am    
Hi Naveen,


UPDATE M SET M.TotalAmount = 2000
FROM billgeneration M
INNER JOIN(select * from billgeneration limit 0,1 ) as d ON D.BillID=M.BillID

Try This

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900