假設您的新id
有AUTO_INCREMENT
此解決方案使用CROSS JOIN
和COALESCE
應該爲你工作。 name
,country
和description
從death
將被忽略,如果提供birth
。
INSERT INTO new_table (name, country, bdate, ddate, description, link)
(
SELECT
COALESCE(b.name, d.name),
COALESCE(b.country, d.country),
b.birthday_date,
d.death_date,
COALESCE(b.description, d.description),
COALESCE(b.link, d.link)
FROM birth b
CROSS JOIN death d ON (d.link = b.link)
)
另外,從birth
插入的所有行:
INSERT INTO new_table (name, country, bdate, ddate, description, link)
(
SELECT name, country, birthday_date, NULL, description, link
FROM birth
)
然後用`death_date'如果有更新的那些行:
UPDATE new_table nt
SET ddate = (SELECT death_date FROM death d WHERE d.link = nt.link)
最後,添加所有行death
,在birth
中沒有輸入:
INSERT INTO new_table (name, country, bdate, ddate, description, link)
(
SELECT name, country, NULL, death_date, description, link
FROM death d
WHERE NOT EXISTS (SELECT NULL
FROM new_table
WHERE link = d.link
)
)
UPDATE NEW_TABLE新臺幣 SET ddate =(SELECT death_date從死亡d WHERE d.link = nt.link) 查詢工作不細.. 它返回以下錯誤 #1048 - 列「ddate '不能爲空 – Vijay 2010-04-04 10:43:33
正如錯誤所說,你的ddate不能爲空。這意味着你只能處理已經死亡的記錄...... – 2010-04-06 06:42:49