在同一查询中的dup上插入INTO,SELECT和UPDATE

时间:2012-12-12 16:37:57

标签: mysql

我正在尝试使用insert和select来更新重复键。

INSERT INTO f_s.s_d_p(dom,title,lastupdated)
SELECT dom AS dDom,
COUNT(db2.rets.dom) AS dcount,
NOW() AS timeNow
FROM db2.rets
GROUP BY db2.rets.dom
LIMIT 5
ON DUPLICATE KEY UPDATE title = dDom, last_updated = NOW();

但是当我运行查询时,我总是得到“未知列dDom”

我尝试为select查询设置别名,但没有运气。

1 个答案:

答案 0 :(得分:2)

使用字段名称,而不是别名:ON DUPLICATE KEY UPDATE title = db2.rets.dom, last_updated = NOW();。或者做

INSERT INTO f_s.s_d_p(dom,title,lastupdated)
SELECT * FROM (
SELECT dom AS dDom,
COUNT(db2.rets.dom) AS dcount,
NOW() AS timeNow
FROM db2.rets
GROUP BY db2.rets.dom
LIMIT 5)
a
ON DUPLICATE KEY UPDATE title = a.dDom, last_updated = NOW();