我有了這個表耐心,必須查詢它MySQL的使用子查詢與更新
Update the database in order to provide a 3% discount to all patients in a room that has more than 2 patients.
表是由如下:
CREATE TABLE patient (
sin varchar(20) NOT NULL,
disease varchar (20),
bed varchar (20),
room varchar (20),
hospital varchar (0),
fee varchar(20),
entry_date date NOT NULL,
exit_date date,
CONSTRAINT FOREIGN KEY (sin) REFERENCES person(sin)
)
所以我想找到所有患者'房間出現超過2名患者,然後更新表格:
UPDATE patient C
INNER JOIN patient D ON C.sin=D.sin and D.sin IN (SELECT A.sin
FROM patient A
WHERE 2 < (SELECT COUNT(B.sin)
FROM patient B
WHERE A.hospital=B.hospital and A.room=B.room and A.exit_date IS NULL and B.exit_date IS NULL)
)
SET C.fee=C.fee*0.97
問題是我得到的錯誤:
You can't specify target table 'C' for update in FROM clause
有沒有辦法使用子查詢更新? 非常感謝。
工作完美,只是組'醫院,房間'而不是團體醫院,房間。如果你可以修改你的答案,所以它是完美的 非常感謝! – icosac