I have a simple requirement, but can’t get the SELECT statement right, so that it will deliver the desired result.
I have two tables: –
- Person table with ID and other miscellaneous info
- A Licences table with the ID to JOIN on and multiple records per person (10-15)
My aim is to get all people with a Blue licence and a White licence (for example).
Using:
SELECT distinctrow person.ID AS client_ID, postcode, locality
FROM (person INNER JOIN licences ON person.ID = licences.client_ID)
WHERE licence LIKE '%Blue%' **OR** licence LIKE '%White%'
of course returns me people that have either, NOT both.
Using:
SELECT distinctrow person.ID AS client_ID, postcode, locality
FROM (person INNER JOIN licences ON person.ID = licences.client_ID)
WHERE licence LIKE '%Blue%' **AND** licence LIKE '%White%'
of course returns no results (how could it?).
This is a really simplified example, as I am pulling 5 tables together in a nested JOIN, that all have criteria (coming in on 40-odd $_GET variables!) … but this is where I am stuck. I have used AND in the INNER JOIN, instead of the WHERE, without any change in the outcome. I’ve drunk lots of beer, still no change 🙂
I know I am revealing my lack of knowledge of even the basics, but surely I don’t need to put SELECTs within SELECTs? LEFT or RIGHT JOIN, perhaps?
I feel I am about to learn something elementary here, so bring it ON!
or
Depending on data distribution (how many licenses per person do you have), one of the queries will be more efficient.