0

I am trying to JOIN 3 Tables(table1, table2 & table3) in Mysql query where I want to pull the matching data from 2 tables(table1 & table2) comparing a Common Column existing in 3 tables(ie. 'PID').

When Joining these 3 tables, there is no data in table1 with the given Date('2012-12-27') then it's returning complete blank row.. Here, I want to get the matching data from the table2 matching the given Date and 'ZERO' or 'NULL' where there is no matching data in the other table ie. table1.. instead of a whole blank row.

Here is the code I was trying that returns a complete BLANK ROW..

SELECT * FROM table3 b
LEFT JOIN table1 r ON r.PID = b.PID
LEFT JOIN table2 a ON ab.PID = b.PID
WHERE b.Name ='stallion' AND r.Date = '2012-12-27' AND a.Date = '2012-12-27'
;
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

0

Use two different JOIN statement then UNION them.

Turcia
  • 653
  • 1
  • 12
  • 29
  • Hi Turcia.. ThankYou for your valuable suggestion :) However, AndreKR's Answer has sorted my problem cleanly. Will, implement your suggestion for sure too. Have a Nice Day!! – user1943364 Jan 03 '13 at 12:33
0

The rows where there is no data in table1 (r) have r.Data = NULL and are therefore filtered away by your WHERE condition. You need to add OR r.Date IS NULL to your WHERE condition or move the condition to the ON clause:

SELECT * FROM table3 b
LEFT JOIN table1 r ON r.PID = b.PID AND r.Date = '2012-12-27'
LEFT JOIN table2 a ON a.PID = b.PID AND a.Date = '2012-12-27'
WHERE b.Name ='stallion';
AndreKR
  • 32,613
  • 18
  • 106
  • 168
  • Hey AndreKR, THANKYOU Very MUCH.. :) You've SOLVED My problem just like that..Your corrections in my code working awesome.. Your HELP is Very Much APPRECIATED!! – user1943364 Jan 03 '13 at 12:13