MySQL的.水平显示属性

是否可以水平显示属性.

例如,我有这样的表:

《MySQL的.水平显示属性》

而且简单

SELECT user.name, project.name
FROM user
  LEFT JOIN project ON user.project_id = project.id;

给我

User #1,Project #1
User #2,Project #1
User #3,Project #2

是否有可能收到这样的结果?在一列项目名称和所有相关用户.

Project #1
User #1
User #2
Project #2
User #3

最佳答案 我认为您可以使用UNION查询以及几个内联列生成所需的输出以对输出进行排序.

SELECT t.name
FROM
(
    SELECT DISTINCT project.name AS name,
                    project.id AS id,
                    0 AS idx
    FROM user
    LEFT JOIN project
        ON user.project_id = project.id
    UNION ALL
    SELECT user.name,
           project.id,
           1
    FROM user
    LEFT JOIN project
        ON user.project_id = project.id
) t
ORDER BY t.id,
         t.idx,
         t.name
点赞