mysql复杂查询|如何从select中删除,如何从数组中创建子列表

7rtdyuoh  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(663)

我正在处理这个表:我开始的表和下一个sql:

SELECT customer_id, 
   GROUP_CONCAT(incident_id order by create_time asc) as ids,
   count(incident_id) as total_count 
FROM incidents 
GROUP BY customer_id

此sql生成下表
我想对生成的表执行删除查询。我试着这么做:

DELETE FROM (
    SELECT customer_id, 
        GROUP_CONCAT(incident_id order by create_time asc) as ids,
        count(incident_id) as total_count 
    FROM incidents 
    GROUP BY customer_id)
WHERE total_count > 3

但我收到一个错误:错误
我做错什么了?
第二个问题-->一旦我知道如何从这个表中删除,我想扩展delete term(即“where total\u count>3”),并添加另一个term,它将删除id大于3的所有行,因此我将只剩下3个id。在postgress中,我将添加下一个术语:incident\u id=any(ids[3:total\u count]),意思是我想从组中创建子列表。如何在mysql中实现它?
要将其升级,我要执行以下sql:

DELETE FROM (
    SELECT customer_id, 
           GROUP_CONCAT(incident_id order by create_time asc) as ids,
           count(incident_id) as total_count 
    FROM incidents 
    GROUP BY customer_id)
WHERE total_count > 3 
     AND incident_id = ANY(ids[3:total_count])
fykwrbwg

fykwrbwg1#

http://sqlfiddle.com/#!9/d14a1da/2号

DELETE i.* FROM incidents i
INNER JOIN (
    SELECT customer_id, 
        count(incident_id) as total_count 
    FROM incidents 
    GROUP BY customer_id
    HAVING total_count >3) f
ON i.customer_id = f.customer_id

相关问题