0

How i put an update in a SELECT-query at once. I wish to count a field from the SELECTed row.

SELECT * FROM `_server` order by hits asc LIMIT 1 (update `_server` set `hits`=hits+1)
dazzafact
  • 2,570
  • 3
  • 30
  • 49
  • 1
    You don't - they're two separate statements. – Siyual Sep 28 '15 at 15:29
  • Not sure what you mean dazzafact. Is the goal of selecting the hits to display it to the user first? If so, @Siyual is right. Otherwise, you might be implying something like this: http://stackoverflow.com/questions/1262786/mysql-update-query-based-on-select-query – Greg Viers Sep 28 '15 at 15:30
  • Which rows you want to update? There is no `WHERE` condition in your query. – Hotdin Gurning Sep 28 '15 at 15:30

1 Answers1

1

You can use order by and limit in update:

update `_server`
    set hits = hits + 1
    order by hits asc
    limit 1;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786