2015-06-13 172 views
0

我有兩個表CHEF和MEAL。 我想要做的是降低每個尚未製作單一餐的廚師的薪水。 這是我的查詢SQL兩個表單行子查詢返回多個行

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);; 

它返回它說:「單行子查詢返回多個行」 我到底做錯了什麼錯誤?

+0

內子查詢'選擇c.Chef_ID FROM CHEFÇLEFT JOIN餐M於(C .Chef_ID = m.Chef_ID) HAVING count(m.Chef_ID)='0' GROUP BY c.Chef_ID'返回多於一行。如果要運行整個更新查詢,它應該只返回一行。運行子查詢獨立,你會發現多個結果正在退回。 – user1

+0

SET Chef_salary = Chef_salary * 0.95可能會給你相同的結果,減少計算,假設減少5%是你正在尋找的 – JamieD77

+0

不需要加入兩張表。更新CHEF c設置Chef_salary = Chef_salary-(Chef_salary/100 * 5) 其中c.Chef_ID不在(從MEAL m中選擇Chef_ID) –

回答

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' 
); 
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 
        ); 
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)