Reputation: 1026
I have two tables:
Table1:
ID | Column 1 | Column 2
1 | Value 1 | Value 2
Table 2:
ID | Column 3 | Column 4
1 | Value 4 | Value 5
1 | Value 6 | Value 7
At the moment my query is:
SELECT * FROM Table1 INNER JOIN Table2 ON Table1.id = Table2.id ;
returns:
id | Column 1 | Column 2 | id | Column 3 | Column 4
1 | Row 1 | Row 1 | 1 | Row 1 | Row 1
1 | Row 1 | Row 1 | 1 | Row 2 | Row 2
1 | Row 1 | Row 1 | 1 | Row 3 | Row 3
I want to use SQL inner join or any other join to return single row from Table1, and all other related rows from Table2
id | Column 1 | Column 2 | id | Column 3 | Column 4
1 | Row 1 | Row 1 | 1 | Row 1 | Row 1
1 | Row 2 | Row 2
1 | Row 3 | Row 3
Is that possible?
Upvotes: 1
Views: 69
Reputation: 1269803
This is too long for a comment.
You can do what you want in SQL, but I discourage you from attempting it. First, it is more cumbersome in MySQL than in other dialects (because of the lack of window functions).
More importantly, it violates one of the key ideas in relational databases: tables and result sets represent unordered sets. The ordering of the rows should not matter. In your result set, there is a big difference between the first row and the rest of the rows. The meaning of many of the rows depends on what comes before it. And, there is no "before" in unordered sets.
Although a nice challenge to get these results in SQL, it is better to do this type of formatting in the application layer. Application programming environments are more suited for adjusting results for presentation.
Upvotes: 1