Note that there are some explanatory texts on larger screens.

plurals
  1. PORemoving duplicates from result of multiple join on tables with different columns in MySQL
    primarykey
    data
    text
    <p>I am trying to make one statement to pull data from 3 related tables (as in they all share a common string index). I am having trouble preventing MySQL from returning the product of two of the tables, making the result set much larger than I want it. Each table has a different number of columns, and I would prefer to not use UNION anyway, because the data in each table is separate. </p> <p>Here is an example:</p> <p>Table X is the main table and has fields A B.</p> <p>Table Y has fields A C D.</p> <p>Table Z has fields A E F G.</p> <p>-</p> <p>My ideal result would have the form:</p> <pre><code>A1 B1 C1 D1 E1 F1 G1 A1 B2 C2 D2 00 00 00 A2 B3 C3 D3 E2 F2 G2 A2 B4 00 00 E3 F3 G3 </code></pre> <p>etc...</p> <p>-</p> <p>Here is the simplest SQL I have tried that shows my problem (that is, it returns the product of Y * Z indexed by data from A:</p> <pre><code>SELECT DISTINCT * FROM X LEFT JOIN Y USING (A) LEFT JOIN Z USING (A) </code></pre> <p>-</p> <p>I have tried adding a group by clause to fields on Y and Z. But, if I only group by one column, it only returns the first result matched with each unique value in that column (ie: A1 C1 E1, A1 C2 E1, A1 C3 E1). And if I group by two columns it returns the product of the two tables again.</p> <p>I've also tried doing multiple select statements in the query, then joining the resulting tables, but I received the product of the tables as output again.</p> <p>Basically I want to merge the results of three select statements into a single result, without it giving me all combinations of the data. If I need to, I can resort to doing multiple queries. However, since they all contain a common index, I feel there should be a way to do it in one query that I am missing.</p> <p>Thanks for any help.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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