Jump to content

MySQL Update... Limit


Err

Recommended Posts

I am trying to make a SQL string that updates based on the WHERE and the LIMIT clause. I'm trying to do:

UPDATE `iconomy` SET `status`='1' WHERE `id` LIKE '%j%' OR `username` LIKE '%j%' OR `balance` LIKE '%j%' OR `status` LIKE '%j%' ORDER BY `username` DESC LIMIT 7, 1

Basically, the above is suppose to update "status" where results are like "j" and are order by "username" in a descending fashion, which pulls many results but I try to only target the 7th result of this query. Problem is, this doesn't work for UPDATE, although it works great for SELECT. Is there a way I can do this using an UPDATE query? The tables are dynamic so there isn't an id or primary key that I can rely on.

Link to comment
Share on other sites

First, that may the weirdest use for a query I've seen. What's the significance of the 7th record? Anyway, one way would be to use two queries where you select the username first then update it. You can do just about all of that in an update query except specify the 7th record. You can specify a limit of 7 and it will update the first 7 records, but you can't tell it to update only one. You can combine the two queries into one with a subquery in the where clause, but it would still be two queries that need to be run.

Link to comment
Share on other sites

It's easier to think of this as a single row being edited after a search was done then a link was clicked to modify that specific row in a database table. That is what makes any x number of result significant. Edit: I should note, I've tried to do this several different ways. You mentioned two queries. This is what I found on my searches:

UPDATE `iconomy` SET `status`='1' WHERE `id` LIKE '%j%' OR `username` LIKE '%j%' OR `balance` LIKE '%j%' OR `status` LIKE '%j%' IN (SELECT * FROM `iconomy` WHERE `id` LIKE '%j%' OR `username` LIKE '%j%' OR `balance` LIKE '%j%' OR `status` LIKE '%j%' ORDER BY `username` DESC LIMIT 7, 1)

The query still doesn't work.

Link to comment
Share on other sites

Thanks for your help. I managed to do this by using the WHERE clause and using the data in the record to act as a reference point, then I just used LIMIT 1 so in cause there are doubles, it only updates one record.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...