为什么这个MySQL语句不起作用?

时间:2018-04-19 21:52:02

标签: mysql group-by

这个SQL语句告诉我组附近有错误... 它适用于select * from和no set。任何想法?

UPDATE  orders o
JOIN (select i.oid, i.pid    from  items i  where i.pid <> '4970'   ) i ON o.oid= i.oid
JOIN products p  ON i.pid = p.product_id 
SET o.sortloc =  concat('zzz',p.location)
where  (o.stat = '4' OR o.stat = '0' OR o.stat = '20'  )
group by o.oid
having  count(*)  > 1  

2 个答案:

答案 0 :(得分:0)

通过将计数*更改为内部选择来解决。

UPDATE 
orders o
JOIN (select i.oid, i.pid, count(*) as ocnt    from  items i  where i.pid <> '4970' group by i.oid) i ON o.oid= i.oid
JOIN products p  ON i.pid = p.product_id 
SET o.sortloc =  concat('zzz',p.location)
where  (o.stat = '4' OR o.stat = '0' OR o.stat = '20'  )  and i.ocnt > 1

答案 1 :(得分:0)

UPDATE!= SELECT

如果您查看两个语句的语法:UPDATESELECT

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
    SET assignment_list
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]

GROUP BY您无法UPDATE结果。当您选择某些内容并使用GROUP BY时,您正在汇总结果。这不是实际数据,因此无法按原样更新。