I’m trying to write a query that combines finding a particular record (identified by primary key) having its status column set to CONFIRMED but can also find whether that primary key exists in the table at all. This is what I tried:
SELECT
count(`p2`.`id`) AS `confirmed`,
count(`p1`.`id`) AS `exists`
FROM `foos` `p2`, `foos` p1`
WHERE
`p2`.`id` = 28 AND
`p2`.`status` = 'CONFIRMED' AND
`p1`.`id` = 28
So again, I’m trying to find if there exists a foo with id = 28 and status = 'CONFIRMED' but I also want to know whether the id doesn’t exist or whether it does exist but the status isn’t confirmed. MySQL says there’s a problem with this query near
`p2`.`id` = 28 AND
`p2`.`status` = 'CONFIRMED'
Is this query possible at all?
I would solve it like this: