說我有這個select語句:與連接表的SQL更新
SELECT ut.user_id,
a.firstname,
a.surname,
u.username,
u.email_address
FROM administrators a
JOIN user_types ut JOIN users u ON a.admin_id = ut.type_id
AND u.user_id = ut.user_id AND ut.user_type = 'ADMIN';
說我想更新的行中的所有值,其中USER_ID = 1;
我可以單獨更新每個表或我可以創建像這樣一個觀點:
CREATE OR REPLACE VIEW full_admin AS
SELECT ut.user_id,
a.firstname,
a.surname,
u.username,
u.email_address
FROM administrators a
JOIN user_types ut
JOIN users u on a.admin_id = ut.type_id
AND u.user_id = ut.user_id
AND ut.user_type = 'ADMIN'
然後,我可以寫:
UPDATE full_admin
SET firstname = 'blah', etc, etc
WHERE user_id = 1;
而且將所有表中更新所有行
有沒有辦法做到這一點,而不創建一個視圖?
喜歡的東西:
UPDATE (
SELECT ut.user_id,
a.firstname,
a.surname,
u.username,
u.email_address
FROM administrators a
JOIN user_types ut
JOIN users u ON a.admin_id = ut.type_id
AND u.user_id = ut.user_id
AND ut.user_type = 'ADMIN'
)
AS temp_table
SET firstname = "ALEX"
WHERE user_id = 1;
然而,這並不工作
如果你想一次更新的管理員和用戶列然後看看這個:http://stackoverflow.com/questions/6362594/a-conditional- SQL更新語句換兩表一次刻錄。如果你只想更新管理員列中的基於用戶的連接,那麼看看這個:http://stackoverflow.com/questions/1293330/how-can-i-do-an-update-statement-with-join-在-SQL。 – dash 2012-02-08 13:24:47