Php 在另一个选择中选择concat组a

Php 在另一个选择中选择concat组a,php,mysql,sql,Php,Mysql,Sql,简单问题: 共有两个表格: 流派[名称,歌曲ID] 歌曲[id、标题、用户id、状态] 从歌曲中选择id、名称、标题、用户id、状态。id=GREEP.songID按id ASC排序 要从中获取结果的查询是什么 +----+-------------+----------------------+--------+--------+ | id | genre.name | song.title | userID | status | +----+-------------+

简单问题:

共有两个表格:

  • 流派[名称,歌曲ID]

  • 歌曲[id、标题、用户id、状态]

    从歌曲中选择id、名称、标题、用户id、状态。id=GREEP.songID按id ASC排序

  • 要从中获取结果的查询是什么

    +----+-------------+----------------------+--------+--------+
    | id | genre.name  | song.title           | userID | status |
    +----+-------------+----------------------+--------+--------+
    |  1 | tech        | Feel it all          |      1 |      1 |
    |  2 | tech        | Tester               |      1 |      1 |
    |  3 | music       | Sejujurnya           |      1 |      1 |
    |  4 | music       | Not Done             |      1 |      1 |
    |  5 | life        | Cinta                |      1 |      1 |
    |  6 | life        | Feel it all          |      1 |      1 |
    |  7 | life        | Not Done             |      1 |      1 |
    |  8 | truth       | Tester               |      1 |      1 |
    |  9 | tree        | Tester               |      1 |      1 |
    | 10 | climb       | Tester               |      1 |      1 |
    +----+-------------+----------------------+--------+--------+
    

    谢谢

    与GROUP BY一起使用

    SELECT 
       id,
       genre.name,
       GROUP_CONCAT(title) as title,
       userID,
       status 
    FROM 
       songs 
    INNER JOIN 
       genre 
    ON 
       song.id=genre.songID 
    GROUP BY 
       genre.name 
    ORDER BY 
       id ASC
    

    我想我看到了一个模式的可能重复,来自同一流派的标题被内爆了,但是
    loud
    buka
    又是怎么回事呢?它们甚至不在原始表中@Declan_K:您的链接是针对SQL server的,这有点不同。@DaveChen,对不起,不得不补表。已经编辑了groupconcat是在genre.name上使用“groupby”所需要的。太棒了。@AFwcxx此处返回的
    id
    字段在这里毫无意义,因为它只引用其中一首歌曲,您可以将concat更改为
    GROUP_concat(concat(id,“-”,title))作为标题,
    这样您仍然可以拥有歌曲的id和标题,并在代码中拆分它们,即
    explode(“-”,$title);
    
    SELECT 
       id,
       genre.name,
       GROUP_CONCAT(title) as title,
       userID,
       status 
    FROM 
       songs 
    INNER JOIN 
       genre 
    ON 
       song.id=genre.songID 
    GROUP BY 
       genre.name 
    ORDER BY 
       id ASC
    
    SELECT
        `Songs`.`id`,
        `Genre`.`Name`,
        GROUP_CONCAT(`Songs`.`Title`) as Title,
        `Songs`.`userID`,
        `Songs`.`status`
    FROM
        `Genre`,
        `Songs`
    WHERE
        `Genre`.`SongID` = `Songs`.`id`
    GROUP BY
        `Genre`.`Name`