MySQL inner join different results

Posted by Darryl at NetHosted on Stack Overflow See other posts from Stack Overflow or by Darryl at NetHosted
Published on 2012-10-12T08:56:18Z Indexed on 2012/10/12 9:37 UTC
Read the original article Hit count: 178

Filed under:

I am trying to work out why the following two queries return different results:

SELECT DISTINCT i.id, i.date  
FROM `tblinvoices` i
INNER JOIN `tblinvoiceitems`  it ON it.userid=i.userid
INNER JOIN `tblcustomfieldsvalues`  cf ON it.relid=cf.relid  
WHERE i.`tax` = 0  
AND i.`date`  BETWEEN  '2012-07-01' AND '2012-09-31'  

and

SELECT DISTINCT i.id, i.date  
FROM `tblinvoices` i
WHERE i.`tax` = 0  
AND i.`date`  BETWEEN  '2012-07-01' AND '2012-09-31'  

Obviously the difference is the inner join here, but I don't understand why the one with the inner join is returning less results than the one without it, I would have thought since I didn't do any cross table references they should return the same results.

The final query I am working towards is

SELECT DISTINCT i.id, i.date  
FROM `tblinvoices` i
INNER JOIN `tblinvoiceitems`  it ON it.userid=i.userid
INNER JOIN `tblcustomfieldsvalues`  cf ON it.relid=cf.relid  
WHERE  cf.`fieldid` =5  
AND  cf.`value` 
REGEXP  '[A-Za-z]'
AND i.`tax` = 0  
AND i.`date`  BETWEEN  '2012-07-01' AND '2012-09-31'  

But because of the different results that seem incorrect when I add the inner join (it removes some results that should be valid) it's not working at present, thanks.

© Stack Overflow or respective owner

Related posts about mysql