I cant seem to work out why:
SELECT `Title`.`Title`, `FirstName`, `LastName`, `Address1`, `Address2`, `Town`.`Town`, `County`.`County`, `PostalCode`, `Phone1`, `Solo`
FROM `Person`
JOIN `Title` ON `Person`.`Title` = `Title`.`id`
JOIN `Town` ON `Person`.`Town` = `Town`.`id`
JOIN `County` ON `Person`.`County` = `County`.`id`
WHERE `Person`.`Solo`='1'
ORDER BY `LastName` ASC;
Returns 0 results, (which it shouldn’t, there are at least 5 results where Solo=’1′)
And if I modify it, to take out all the JOINS:
SELECT `Title`, `FirstName`, `LastName`, `Address1`, `Address2`, `Town`, `County`, `PostalCode`, `Phone1`, `Solo`
FROM `Person`
WHERE `Person`.`Solo`='1'
ORDER BY `LastName` ASC;
I get Results? If I modify it to take out the WHERE Person.Solo='1' It gives me results too, but only 2; where surley it should return ALL the rows? Can someone explain?
🙂
note: Solo is a tinyint, it equels either 0 or 1 in all the records!
Your joins are strict, it means that if
Persondoesn’t haveTitleorTownorCountrythen it wouldn’t be returned. You don’t get any results even if you remove wholeWHEREclause.Try
LEFT JOINinstead.This is good explanation of how different type of joins work: A Visual Explanation of SQL Joins