我有一个MySQL查询,它提供以下结果,
mysql - MySQL将行转换为列-LMLPHP
使用的查询是

SELECT  modi, aid FROM (SELECT
 GROUP_CONCAT(id) his,
an_id aid,
GROUP_CONCAT(m_id) modi
FROM
ah
GROUP BY an_id, m_id) t1
GROUP BY aid, modi
order by aid;

现在我需要一个mysqlquery which will give me a result set where if columnaidhas the same value in two rows should create a new column with sameaidandmodi`。
例如,图像中的前两行相同但不同。我需要的结果应该有3列,行应该是,
aid           modi                  modi2
27     6,6,6,6,6,6,6,6,6              8

我对sql很陌生。提前谢谢

最佳答案

一种方法是:

SELECT aid, min(modi) as modi1, max(modi) as modi2
FROM (SELECT an_id as aid,
             GROUP_CONCAT(m_id) as modi
      FROM annotation_history ah
      GROUP BY an_id
    ) t1
GROUP BY aid
HAVING min(modi) <> max(modi);

08-25 12:51