AnchovyLegend
AnchovyLegend

Reputation: 12538

Update statement with ORDER BY

I am having a hard time writing an UPDATE query that includes an ORDER BY clause.

Is this possible? I would appreciate any suggestions on how to do this.

This is a mock up of what I am trying to do:

UPDATE notes SET `note`='$note' WHERE `date` = (ORDER BY `date`  DESC LIMIT 1)

Many thanks in advance!

Upvotes: 0

Views: 92

Answers (3)

ljh
ljh

Reputation: 2594

you dont' really need order by, basing your sample query.


Update .... 'date' = (select max('date') from notes)

Upvotes: 0

user650749
user650749

Reputation: 182

Try this sql query:

UPDATE notes SET `note`='$note' WHERE `date` = (select `date` from notes ORDER BY DESC LIMIT 1)

Upvotes: 1

hd1
hd1

Reputation: 34657

You are missing a column in your update statement. This fiddle shows you how to do it. Basically, you need to add date to your order by clause.

Upvotes: 1

Related Questions