Note that there are some explanatory texts on larger screens.

plurals
  1. POMysql Left join multiple 1 to many tables
    primarykey
    data
    text
    <p>I am trying to get the following data from 3/4 tables in 1 <code>Mysql query</code>, wondering if it is possible ? The tables are</p> <pre><code>TOPIC topicid (FK)(PK) groupid topic user LIKED likeid topicid (FK) user COMMENT commentid (PK) topicid (FK) comment user </code></pre> <p>I write my topics and store in TOPIC Table with unique topicid. I group each topic using groupid. Other tables may have 0 or more data per topicid.</p> <p>I am trying to get each topic for a particular group and also get other datas from the concerned Tables. I checked <a href="https://stackoverflow.com/questions/4650914/how-to-left-join-multiple-one-to-many-tables-in-mysql">How to left join multiple one to many tables in mysql?</a> and got few idea but that is for the count while I wanted to get details from that table (users who like), and (user and their comment).</p> <p>I have tried</p> <pre><code>SELECT t.topicid, topic, group_concat(DISTINCT likeid,l.user SEPARATOR '|'), group_concat(DISTINCT commentid,comment,c.user SEPARATOR '|') AS comments FROM TOPIC t LEFT JOIN LIKE l ON l.topicid = t.topicid LEFT JOIN COMMENT c ON c.topicid = t.topicid WHERE t.groupid='some_value' GROUP BY t.topicid </code></pre> <p>While this works partly e.g. I do get the details but only if there is one topic in a group. If there are 2 or more topics in a group then the <code>concat</code> details are stored in the first record only and the later topics show no likes and comments. </p> <p>Can someone please help me to correct this or any particular <code>Mysql function</code> I am missing</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload