如何使以下查询按预期工作?MySQL对multipleORDER BY

UPDATE user_list
SET user_code = $code
WHERE user_id = $id
AND country = $country AND
     ((d = $d) OR
     (c = $c ORDER BY c ASC LIMIT 1) OR
     (b = $b ORDER BY b ASC LIMIT 1))

想法如下:
如果有user_id = $id AND country = $country AND d = $d的用户,则SET user_code = $code
如果以上是错误的,则转到c命令的第一个用户user_id = $id AND country = $country AND c = $c并执行SET user_code = $code
如果以上是错误的,那么去把b命令的第一个用户带到user_id = $id AND country = $country AND b = $b中,然后做SET user_code = $code

最佳答案

您可以使用嵌套sql来查找按c或b排序的第一个用户,我修改了您原来的命令。

UPDATE user_list
SET user_code = $code
WHERE user_id = $id
AND country = $country AND
     ((d = $d) OR
     ( $c = (select c from user_list order by c asc limit 1)) OR
     ( $b = (select b from user_list order by b asc limit 1)))

关于php - mysql更新查询有多个顺序由?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26907590/

10-16 10:36