更新查询不只一件事

时间:2019-04-18 20:57:47

标签: mysql

更新具有最低值的S_Loc,S_Code。

我不断出错 错误代码:1064您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在库中的附近使用。S_Code = s.S_Code

Update library 
inner join (
    Select S_Code 
    from Book 
    group by S_Code 
    having count(xy) = min(count(xy)) s 
on Book.S_Code = s.S_Code 
set S_Loc = 'CB-3322';

它使用重复最少的s_code更改值,以拥有新的s_loc。

1 个答案:

答案 0 :(得分:0)

您不能嵌套MIN(COUNT(ISBN))之类的聚合函数。您需要另一个子查询来获取它。

Update library 
inner join (
    Select S_Code 
    from Library 
    group by S_Code 
    having count(*) = (SELECT COUNT(*) AS c FROM library GROUP BY S_Code ORDER BY c LIMIT 1)) s 
on library.S_Code = s.S_Code 
set S_Loc = 'CB-3322';