0

由于关系是一对一的,因此我已将连接表与其相关实体合并。

所以现在原始ww_staffww_contacts直接保存了详细信息。

我根据我认为从 MySQL 的角度来看合乎逻辑的内容编写了以下语句,
但是 - 它并不快乐。

任何人都可以看到类似的解决方案或公然的违法行为吗?

INSERT INTO 
  ww_staff s 
  (phone, mobile, email, skype) 
VALUES 
(
  SELECT w.phone, w.mobile, w.email, w.skype 
  FROM ww_contacts w
  JOIN ww_staff s
  ON s.staff_ID = w.contacts_ID
);
4

2 回答 2

2

只需删除 VALUES()

INSERT INTO ww_staff s (phone, mobile, email, skype) 
SELECT w.phone, w.mobile, w.email, w.skype FROM ww_contacts w
JOIN ww_staff s
ON s.staff_ID = w.contacts_ID;

- 更新

由于您正在选择ww_contacts w JOIN ww_staff- 所有记录都已经存在 - 并且您不想插入重复项,请使用带有连接的更新:

UPDATE ww_staff s JOIN ww_contacts w ON s.staff_ID = w.contacts_ID
SET s.phone = w.phone, s.mobile = w.mobile, s.email = w.email, s.skype = w.skype;

下次请在你的问题中解释更多你想要做什么。

于 2010-12-04T08:20:50.663 回答
0

你需要做一个INSERT ... SELECT ... ON DUPLICATE KEY UPDATE声明。这将插入新行并更新现有行:

INSERT INTO 
  ww_staff
  (staff_ID, phone, mobile, email, skype) 
SELECT w.contacts_ID, w.phone, w.mobile, w.email, w.skype 
  FROM ww_contacts w
  JOIN ww_staff s
  ON s.staff_ID = w.contacts_ID
ON DUPLICATE KEY UPDATE
 ww_staff.phone = w.phone, ww_staff.mobile = w.mobile, ww_staff.email = w.email, ww_staff.skype = w.skype
于 2010-12-04T20:09:28.837 回答