I have the following MySQL tables:
tbl_pet_owners:
+----+--------+----------+--------+--------------+
| id | name | pet | city | date_adopted |
Does MySQL STILL not have CTEs? They make life so much easier.
Regardless, to get 'NOT FOUND', use coalesce()
in your SELECT
.
SELECT DISTINCT p.petType
, coalesce(po.name,'NOT FOUND') AS name
, coalesce(po.city,'NOT FOUND') AS city
FROM (
SELECT 'mammal' AS petType UNION ALL
SELECT 'fish' UNION ALL
SELECT 'amphibian' UNION ALL
SELECT 'seacreature'
) p
LEFT OUTER JOIN (
SELECT tpo.name, tpo.city, pt.type AS petType
FROM tbl_pet_owners tpo
LEFT OUTER JOIN tbl_pet_types tpt ON tpo.pet = tpt.pet
) po ON p.petType = po.petType
I don't have a MySQL machine to test this on, but something like this should give you what you're looking for.