SQL两个表单行子查询返回多行

时间:2015-06-13 19:08:18

标签: sql oracle

我有两张桌子,CHEF和MEAL。 我想做的是减少每个尚未做过一顿饭的厨师的工资。 这是我的查询

UPDATE CHEF
SET Chef_salary = Chef_salary-(Chef_salary/100 * 5)
WHERE Chef_ID=(SELECT c.Chef_ID
FROM CHEF c LEFT JOIN MEAL m ON (c.Chef_ID=m.Chef_ID)
HAVING count(m.Chef_ID)='0'
GROUP BY c.Chef_ID);;

它返回一个错误,表示“单行子查询返回多行” 我做错了什么?

3 个答案:

答案 0 :(得分:0)

UPDATE CHEF
SET Chef_salary = Chef_salary-(Chef_salary/100 * 5)
WHERE Chef_ID in (SELECT c.Chef_ID
FROM CHEF c LEFT JOIN MEAL m ON (c.Chef_ID=m.Chef_ID)
GROUP BY c.Chef_ID
HAVING count(m.Chef_ID)='0'
);

答案 1 :(得分:0)

如果我理解你的逻辑,你需要NOT EXISTS和相关的子查询:

UPDATE CHEF
     SET Chef_salary = Chef_salary - (Chef_salary/100 * 5)
     WHERE NOT EXISTS (SELECT 1
                       FROM MEAL m
                       WHERE chef.Chef_ID = m.Chef_ID
                      );

答案 2 :(得分:0)

Update CHEF c Set Chef_salary = Chef_salary-(Chef_salary/100 * 5)
Where c.Chef_ID not in (Select Chef_ID from MEAL m)