简体   繁体   English

使用2个表合并具有相同id的mysql行

[英]Merge mysql rows with same id using 2 tables

I have a mysql database with 2 tables: songideen, kommentare. 我有一个带有2个表的mysql数据库:songideen,kommentare。 In "songideen" you can store songs, in "kommentare" you can comment them. 在“songideen”中你可以存储歌曲,在“kommentare”你可以评论它们。 Both tables are linked via LEFT JOIN. 两个表都通过LEFT JOIN链接。 How to merge the rows (in case a song has 2 or more comments) so all comments are seperated by ',' within one row? 如何合并行(如果一首歌有2个或更多的评论)所以所有评论都在一行中用','分隔? In this example I want to combine the second and the third row: 在这个例子中,我想组合第二行和第三行:

Name   Arbeitstitel    Datum      mp3    ID  Kommentare    KommentarID
Lukas   Titeltest    2016-06-06  Link     1   comment      1 
Jannik  Titeltest2   2016-07-06  Link2    2   comment2     2
Jannik  Titeltest2   2016-07-06  Link2    2   comment3     2
Andi    Titeltest3   2016-07-20  Link3    3   comment4     3

I alrady tried it this way, but it doesn't work: 我alrady以这种方式尝试过,但它不起作用:

 $sql = "SELECT songideen.Name, songideen.Arbeitstitel, songideen.Datum, songideen.mp3, songideen.ID, GROUP_CONCAT(kommentare.Kommentar SEPARATOR ',') AS KommentarIDs, kommentare.KommentarID
                FROM songideen
                LEFT JOIN kommentare
                ON songideen.ID=kommentare.KommentarID
                GROUP BY kommentare.KommentarID";

You needed to group by songideen.ID . 你需要按照songideen.ID进行分组。 Since there are records in the songideen table without any comment in kommentare table. 由于在记录songideen表没有任何评论kommentare表。

SELECT
    songideen.Name,
    songideen.Arbeitstitel,
    songideen.Datum,
    songideen.mp3,
    songideen.ID,
    GROUP_CONCAT(kommentare.Kommentar SEPARATOR ',') AS KommentarIDs,
    kommentare.KommentarID
FROM songideen
LEFT JOIN kommentare ON songideen.ID = kommentare.KommentarID
GROUP BY songideen.ID ;

Note: 注意:

In your case you were grouping the result set by kommentare.kommentarID . 在您的情况下,您正在通过kommentare.kommentarID对结果集进行分组。 For records in songideen table which don't have any comment in the kommentare table will result in NULL value of kommentare.kommentarID . 对于记录songideen表,该表没有在任何评论kommentare表将导致NULLkommentare.kommentarID Thus your final result set will consist of those records which have corresponding comments in the kommentare table plus one entry for all the NULL entries found in kommentare table. 因此,最终的结果集将包括那些有相应的意见记录kommentare表加上所有的一个条目NULL中找到的条目kommentare表。

use following queries for your output. 使用以下查询输出。

$sql = "SELECT s.Name, s.Arbeitstitel, s.Datum, s.mp3, s.ID, GROUP_CONCAT(k.Kommentar SEPARATOR ',') AS KommentarIDs, k.KommentarID,(SELECT count(*) FROM  kommentare  where KommentarID= s.ID) as commentCount
                FROM songideen s , kommentare k
                where s.ID=k.KommentarID            
                GROUP BY k.KommentarID
                    having commentCount>2
                ";

explanation: 说明:

here we are using inner join you will not get any duplicates. 在这里我们使用内连接你不会得到任何重复。 and using having you can restrict number comments 使用你可以限制数字评论

SELECT
  *
FROM songideen
LEFT JOIN (SELECT
  GROUP_CONCAT (kommentare SEPARATOR ','), KommentarID
FROM kommentare
GROUP BY KommentarID) AS kommentare
  ON kommentare.KommentarID = songideen.id

LEFT JOIN on a subquery which returns the concatenated kommentare grouped by the KommentarID . LEFT JOIN在子查询上返回按KommentarID分组的连接的kommentare

But tbh, you should rework your table design, or at least change some names of columns. 但是,你应该重做你的表设计,或者至少改变一些列的名称。

kommentare . kommentare KommentarID - This doesn't seem to be the actual identifier for kommentare , but is the id of the corresponding songideen . KommentarID - 这似乎不是kommentare的实际标识符,而是相应songideen的id。 id , so this should be called songideenID so you can easily see, this is the column which builds the relation between the tables. id ,所以这应该被称为songideenID所以你可以很容易地看到,这是建立表之间关系的列。

Missing "actual" KommentarID - what is the primary key in your kommentare table? 缺少“实际”KommentarID - 你的kommentare表中的primary key是什么? An auto_increment int primary key would feel very intuitively. auto_increment int primary key会非常直观地感受到。

It would then look loke this: http://sqlfiddle.com/#!9/150e7/4 然后它会看起来像这样: http ://sqlfiddle.com/#!9 / 150e7 / 4

SELECT
  *
FROM songideen 
LEFT JOIN (SELECT
  GROUP_CONCAT(Kommentare SEPARATOR ',') AS kommentar, songideenID
FROM kommentare 
GROUP BY songideenID) AS kommentare USING(songideenID)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM