Mysql行转化为列,使用IF优化查询

发布于 2024-10-19 08:02:16 字数 781 浏览 4 评论 0原文

我有以下查询:

SELECT a.rank AS rank, concat( m.prenom, ' ', m.nom ) AS name, d.department
FROM `0_area` AS a
LEFT JOIN 0_member AS m ON a.user_id = m.id
LEFT JOIN 0_depart AS d ON a.user_id = d.user_id
WHERE a.sec_id = 2
AND (a.rank = 'mod' OR a.rank = 'adm')
AND d.department IN ( 75, 92 )

上面的查询返回三列,命名 rank |名称 |部门 并且它正在工作,针对部门的每个级别都有一个单独的行。

rank | name | department

mod  |  Steven Smith   |  75   
mod  |  Jeremy Roy     |  92  
adm  |  Vincent Jones  |  75  

我需要的是将所有这些信息放在一行中,因为我需要将其作为另一个更大查询的一部分,即仅返回一行。

我想到了 group_concat,并有两列,一列用于 mod,一列用于 adm。如果有多个 mod(给定示例为 75、92),它们将以逗号分隔。

mod  | adm   

Steven Smith, Jeremy Roy   |  Vincent Jones

我说清楚了吗?谢谢朋友们。

I have this following query:

SELECT a.rank AS rank, concat( m.prenom, ' ', m.nom ) AS name, d.department
FROM `0_area` AS a
LEFT JOIN 0_member AS m ON a.user_id = m.id
LEFT JOIN 0_depart AS d ON a.user_id = d.user_id
WHERE a.sec_id = 2
AND (a.rank = 'mod' OR a.rank = 'adm')
AND d.department IN ( 75, 92 )

The above query is returning me three columns, naming rank | name | department and it's working, with a separate row for each rank against a department.

rank | name | department

mod  |  Steven Smith   |  75   
mod  |  Jeremy Roy     |  92  
adm  |  Vincent Jones  |  75  

What I need, is to get all those information in one row, because I need to have it as a part of another bigger query, that is returning one row only.

I thought of group_concat, and to have two columns, one for mod and one for adm. In case there are multiple mods (in 75, 92 for the given example), they will be comma separated.

mod  | adm   

Steven Smith, Jeremy Roy   |  Vincent Jones

Have I been clear? Thanks friends.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_蜘蛛 2024-10-26 08:02:16

像这样的东西:

SELECT
    a.rank AS rank,
    concat( m.prenom, ' ', m.nom ) AS name,
    GROUP_CONCAT(d.department ORDER BY d.department ASC SEPARATOR '|')
FROM
    `0_area` AS a
LEFT JOIN
    0_member AS m
ON
    a.user_id = m.id
LEFT JOIN
    0_depart AS d
ON
    a.user_id = d.user_id
WHERE
    a.sec_id = 2
AND
    (a.rank = 'mod' OR a.rank = 'adm')
AND
    d.department IN ( 75, 92 )
GROUP BY
    a.rank

Something like:

SELECT
    a.rank AS rank,
    concat( m.prenom, ' ', m.nom ) AS name,
    GROUP_CONCAT(d.department ORDER BY d.department ASC SEPARATOR '|')
FROM
    `0_area` AS a
LEFT JOIN
    0_member AS m
ON
    a.user_id = m.id
LEFT JOIN
    0_depart AS d
ON
    a.user_id = d.user_id
WHERE
    a.sec_id = 2
AND
    (a.rank = 'mod' OR a.rank = 'adm')
AND
    d.department IN ( 75, 92 )
GROUP BY
    a.rank
独自唱情﹋歌 2024-10-26 08:02:16

一些谷歌搜索告诉我,我需要这个 sql:

SELECT 
GROUP_CONCAT( if( a.rank='mod', concat( m.prenom, ' ', m.nom ), '' ) ) AS 'mod', 
GROUP_CONCAT( if( a.rank='adm', concat( m.prenom, ' ', m.nom ), '' ) ) AS 'adm'
FROM `0_area` AS a
LEFT JOIN 0_member AS m ON a.user_id = m.id
LEFT JOIN 0_depart AS d ON a.user_id = d.user_id
WHERE a.sec_id =2
AND (a.rank = 'mod' OR a.rank = 'adm' )
AND d.department IN ( 75, 92 )
GROUP BY a.sec_id

@ToonMariner 这解决了这个查询的问题,但是,我仍然需要将两个查询放在一起。也许我会为此创建一篇新帖子。

Some googling told me that I need this sql:

SELECT 
GROUP_CONCAT( if( a.rank='mod', concat( m.prenom, ' ', m.nom ), '' ) ) AS 'mod', 
GROUP_CONCAT( if( a.rank='adm', concat( m.prenom, ' ', m.nom ), '' ) ) AS 'adm'
FROM `0_area` AS a
LEFT JOIN 0_member AS m ON a.user_id = m.id
LEFT JOIN 0_depart AS d ON a.user_id = d.user_id
WHERE a.sec_id =2
AND (a.rank = 'mod' OR a.rank = 'adm' )
AND d.department IN ( 75, 92 )
GROUP BY a.sec_id

@ToonMariner This solves the problem for this query, however, I will still need to put two queries together. Probably I will create a new post for this one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文