0

I have this query:

SELECT a.total
     , b.total
     , (a.total - b.total) as dv
     , a.customer_name
     , a.report_date
     , b.report_date 
  FROM topsourcesfcy a 
 CROSS 
  JOIN topsourcesfcy b 
 WHERE a.customer_name = b.customer_name 
   AND a.customer_name = 'SPECIALTY DRILLING FLUIDS LIMITED'
   AND a.report_date   = '2016-2-10' 
   AND b.report_date   = '2016-2-9' 
   AND a.report_date!  = b.report_date

but the problem is sometimes the record might exist in a and not in b or in b and not in a, which will cause the query to execute to false but I still want to select it if it exists in only one or both. Thanks.

Strawberry
  • 33,750
  • 13
  • 40
  • 57
Ayo K
  • 1,719
  • 2
  • 22
  • 34

1 Answers1

0

Read more thorough answers here: Full Outer Join in MySQL

What you want is a full outer join, but mysql doesn't support it.

Instead you can use a combo of two outer joins (left and right) and a union, the following will also remove dupes:

SELECT *
FROM `t1`
LEFT OUTER JOIN `t2` ON `t1`.`id` = `t2`.`id`

UNION

SELECT *
FROM `t1`
RIGHT OUTER JOIN `t2` ON `t1`.`id` = `t2`.`id`
WHERE `t1`.`id` IS NULL;
Andrew Kuklewicz
  • 10,621
  • 1
  • 34
  • 42