I am having trouble with a MySQL query (version 5.6.37). I think it merely needs a reorganizing of the query components, but I can not make it work.
Problem: when my JOIN returns no rows, the entire query returns no rows, even though the data matches the query.
Here is my current query (where '@' is the input):
SELECT pets.id,pet,collar,GROUP_CONCAT(petData.fleas) AS f_id
FROM titles
JOIN petWear ON pets.id = petWear
JOIN petData ON petWear.id = petData.id
WHERE pets.id = '@'
GROUP BY pets.id,pet,collar
Assuming a "pets" table like this:
id | pet
1 | cat
2 | dog
3 | fish
4 | snake
5 | rabbit
And a JOINed "petData" table like this:
id | fleas
1 | 1
1 | 2
1 | 3
1 | 4
2 | 5
Successful query: If @ = 1, then the query returns a single result:
id | pet | collar | f_id
1 | cat | gold | 1,2,3,4
Unsuccessful query: If @ = 5, then the query returns no result.
What I would like to have returned (for @ = 5) is this single result (i.e. no result, or NULL, for "f_id"):
id | pet | collar | f_id
5 | rabbit | red |
Note that I have included the petWear table with the "collar" listing, just to state that a "normal" join needs to also be part of the picture.