PDA

View Full Version : INSERT/UPDATE a row by its position in the table?


WA
12-05-2002, 03:37 AM
Is there a way to insert or do an update of a row (aka record) by its position in the table, for example, the 5th row of the table? I know you can select data in such a manner, using the LIMIT 4,1 clause, for example. Does this work with insert/update as well, and if so, what's an example SQL syntax?

Thanks!

Spookster
12-05-2002, 04:26 AM
For INSERT and UPDATE the LIMIT keyword allows you to only limit the number of records inserted or udpated unlike SELECT which will allow you to specify a range of rows using LIMIT.

bcarl314
12-05-2002, 12:35 PM
Just a guess, but what about something like,

UPDATE table SET field1='value1', field2='value2', field3='value3' WHERE count(*)=5;

WA
12-05-2002, 12:53 PM
That wouldn't work bcarl314, as count(*) doesn't select a particular row, but merely returns a whole new row containing the total number of rows of the table in question.

So I guess there's no way to update/insert by ordinal number as far the row in question?

Ökii
12-05-2002, 02:18 PM
Why not just pull the row id with a limit clause and use that to define the row to change.

SELECT row_id FROM data WHERE x>'7' LIMIT 4,1

UPDATE data SET x='6' WHERE row_id='$var_from_first_query'

As select is so fast in mysql you wouldn't be doing much resource harm by running two queries.