Jump to content


Photo

How to order in UPDATE when using limit by?


  • Please log in to reply
2 replies to this topic

#1 Jessica

Jessica
  • Staff Alumni
  • This is not my name.
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 10 July 2006 - 02:03 PM

I am performing this query:
$update_item = "UPDATE stall_transactions SET cleared = 1 WHERE stall_id = $stall_id LIMIT 50";

I want it to update the 50 most recent by either sold (datetime) or transaction_id (primary key).

How can I ensure it does the 50 most recent?

Thanks!
ps: the new forum design is awesome!
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#2 designationlocutus

designationlocutus
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 10 July 2006 - 02:31 PM

Baffled me for a minute, but I was sure it was possible, so I had a look over at mysql.com.

The following is the syntax for UPDATE. As long as you get the order correct, I'm sure you will have no problems.

UPDATE [LOW_PRIORITY] [IGNORE] tbl_name
    SET col_name1=expr1 [, col_name2=expr2 ...]
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]



#3 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 10 July 2006 - 04:52 PM

Yup... just add the necessary ORDER BY clause and you're set.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users