How can I join multiple SQL tables using the IDs? How can I join multiple SQL tables using the IDs? mysql mysql

How can I join multiple SQL tables using the IDs?


You want something more like this:

SELECT TableA.*, TableB.*, TableC.*, TableD.*FROM TableA    JOIN TableB        ON TableB.aID = TableA.aID    JOIN TableC        ON TableC.cID = TableB.cID    JOIN TableD        ON TableD.dID = TableA.dIDWHERE DATE(TableC.date)=date(now()) 

In your example, you are not actually including TableD. All you have to do is perform another join just like you have done before.

A note: you will notice that I removed many of your parentheses, as they really are not necessary in most of the cases you had them, and only add confusion when trying to read the code. Proper nesting is the best way to make your code readable and separated out.


SELECT     a.nameA, /* TableA.nameA */    d.nameD /* TableD.nameD */FROM TableA a     INNER JOIN TableB b on b.aID = a.aID     INNER JOIN TableC c on c.cID = b.cID     INNER JOIN TableD d on d.dID = a.dID WHERE DATE(c.`date`) = CURDATE()


You have not joined TableD, merely selected the TableD FIELD (dID) from one of the tables.