简体   繁体   中英

Merge mysql rows with same id using 2 tables

I have a mysql database with 2 tables: songideen, kommentare. In "songideen" you can store songs, in "kommentare" you can comment them. Both tables are linked via 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? 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:

 $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 . Since there are records in the songideen table without any comment in kommentare table.

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 . For records in songideen table which don't have any comment in the kommentare table will result in NULL value of kommentare.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.

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 .

But tbh, you should rework your table design, or at least change some names of columns.

kommentare . KommentarID - This doesn't seem to be the actual identifier for kommentare , but is the id of the corresponding songideen . id , so this should be called songideenID so you can easily see, this is the column which builds the relation between the tables.

Missing "actual" KommentarID - what is the primary key in your kommentare table? An auto_increment int primary key would feel very intuitively.

It would then look loke this: 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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