Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use <code>LEFT JOIN</code> instead of <code>INNER JOIN</code>. The two joins are different from each other. The <code>INNER JOIN</code> (<em>which you are currently using</em>) only returns the records which both has at least one match on either side of the table. And that is why <code>computer 14</code> won't should because it has no match on table <code>COMPUTER_HARDDRIVES</code>. <code>LEFT JOIN</code> on the other hand, returns all records on the <em>left hand side</em> table whether it no match on the other side of the tables.</p> <pre><code>SELECT a.ID AS UserID, a.Name as UserName, b.ID as ComputerID, d.ID as ProcessorID, d.Name as ProcessorName, f.ID as HardDriveID, f.name as HardDriveName FROM users a INNER JOIN computers b ON a.ID = b.user_ID LEFT JOIN computer_processors c ON b.ID = c.C_ID LEFT JOIN PROCESSORS d ON c.p_ID = d.ID LEFT JOIN COMPUTER_HARDDRIVES e ON b.ID = e.c_ID LEFT JOIN HARDDRIVE f ON e.h_ID = f.ID WHERE a.ID = 1 </code></pre> <ul> <li><a href="http://sqlfiddle.com/#!2/f2ab6/1" rel="nofollow"><strong><em>See SQLFiddle Demo</em></strong></a></li> <li>for more details about joins, see <a href="http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins" rel="nofollow"><strong>Visual Representation of SQL Joins</strong></a></li> </ul> <p>and since you want to groupt the rows together, youcan take advantage of <code>GROUP_CONCAT()</code> function. basically, what it does is it combines the value of the columns into comma separated value</p> <pre><code>SELECT a.ID AS UserID, a.Name as UserName, b.ID as ComputerID, GROUP_CONCAT(DISTINCT d.ID) as ProcessorID, GROUP_CONCAT(DISTINCT d.Name) as ProcessorName, GROUP_CONCAT(DISTINCT f.ID) as HardDriveID, GROUP_CONCAT(DISTINCT f.name) as HardDriveName FROM users a INNER JOIN computers b ON a.ID = b.user_ID LEFT JOIN computer_processors c ON b.ID = c.C_ID LEFT JOIN PROCESSORS d ON c.p_ID = d.ID LEFT JOIN COMPUTER_HARDDRIVES e ON b.ID = e.c_ID LEFT JOIN HARDDRIVE f ON e.h_ID = f.ID WHERE a.ID = 1 GROUP BY UserID, UserName, ComputerID </code></pre> <ul> <li><a href="http://sqlfiddle.com/#!2/5bca8/1" rel="nofollow"><strong><em>See SQLFiddle Demo</em></strong></a></li> </ul>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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