mysql转换行到列

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

我有一个 MySQL 提供以下结果的查询,

使用的查询是

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;

现在我需要一个mysql query which will give me a result set where if column 帮助 has the same value in two rows should create a new column with same 帮助 and 莫迪。
例如,图像中的前两行是相同的 aid 但不一样 modi . 我需要的结果应该有3列,行应该是这样的,

aid           modi                  modi2
27     6,6,6,6,6,6,6,6,6              8

我对sql非常陌生。提前谢谢

ccrfmcuu

ccrfmcuu1#

一种方法是:

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);

相关问题