简体   繁体   中英

SQL Incorrect syntax near the keyword 'order'

I am writing in SQL and trying to update a new colunm "rankFeild" in table "seker_w_adress". The updating is just fine, but when I am trying to order the rows selected so the colunm will get the biggest value exist for this ID, I am getting this error:

Incorrect syntax near the keyword 'order'.

Here is my code:

update seker_w_adress set rankFeild=si
from seker_w_adress as swa
join
(
select ID,gender,age,seker_ind as si  
from last_seker
)ls2
on swa.ID=ls2.ID and ((ls2.gender='female' and ls2.age<47) or (ls2.gender='male' and ls2.age<45))
order by ls2.si desc;

I tried to put "order by" into the ls2 brackets, like that:

update seker_w_adress set rankFeild=si
from seker_w_adress as swa
join
(
select ID,gender,age,seker_ind as si  
from last_seker
order by ls2.si desc
)ls2
on swa.ID=ls2.ID and ((ls2.gender='female' and ls2.age<47) or (ls2.gender='male' and ls2.age<45))
;

but it produces another error. What's the problem?

I found the answer - thanks to Akina :

update seker_w_adress set rankFeild=si
from seker_w_adress as swa
join
(
select ID,max(seker_ind) as si  
from last_seker
group by ID
)ls2
on swa.ID=ls2.ID 
join last_seker as ls
on ls.ID=ls2.ID and ((ls.gender='female' and ls.age<47) or (ls.gender='male' and ls.age<45));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM