似乎需要GROUP BY的SQL更新

时间:2011-02-09 21:36:41

标签: sql mysql

我想要更新表格,例如

UPDATE mytable
LEFT JOIN worker ON first=worker_first_name && last=worker_last_name
GROUP BY CONCAT(first, '%%', last)
SET taint = COUNT(pkey) > 1;

但是UPDATE中的table_reference当然不允许GROUP BY。我该如何设置此列?它只是检查第一个/最后一个名称组合是否在另一个表中最多出现一次。

2 个答案:

答案 0 :(得分:3)

UPDATE mytable
INNER JOIN (
  select first, last, COUNT(pkey) > 1 Result
  FROM mytable
  LEFT JOIN worker ON first=worker_first_name AND last=worker_last_name
  GROUP BY first, last) OTHER
    on OTHER.first = mytable.first
   and OTHER.last = mytable.last
SET taint = OTHER.Result;

答案 1 :(得分:2)

类似的东西:

UPDATE mytable
SET taint = ((select count(pkey) from worker where worker_first_name = first and worker_last_name = last) > 1)

但我猜测性能可能会留下一些不足之处。可能会有更高效的方式。