I have the following three SQL queries. I would like to combine them into one is possible, either using the current method, or another method if it would be more efficient.
NOTE: I need to know the distinct count values for all three WHERE clauses.
Query 1:
SElECT COUNT(*) AS imgCount FROM (
SELECT imgHeight, imgWidth, imgId AS primaryId FROM primary_images
UNION ALL
SELECT imgHeight, imgWidth, primaryId FROM secondary_images
) AS union_table
WHERE primaryId = $imgId AND imgWidth = $maxImageWidth AND imgHeight = $maxImageHeight;
QUERY 2:
SElECT COUNT(*) AS imgCount FROM (
SELECT imgHeight, imgWidth, imgId AS primaryId FROM primary_images
UNION ALL
SELECT imgHeight, imgWidth, primaryId FROM secondary_images
) AS union_table
WHERE primaryId = $imgId AND imgWidth = $maxImageWidth AND imgHeight != $maxImageHeight;
QUERY 3:
SElECT COUNT(*) AS imgCount FROM (
SELECT imgHeight, imgWidth, imgId AS primaryId FROM primary_images
UNION ALL
SELECT imgHeight, imgWidth, primaryId FROM secondary_images
) AS union_table
WHERE primaryId = $imgId AND imgWidth != $maxImageWidth AND imgHeight = $maxImageHeight;
Also, my database is MySQL. I have heard that using != for “not equal” doesn’t work in all cases, but that <> is better. In my case, should != be fine?
This might work for you.
I also believe this is equivalent, although I’m not sure.
As per the request, here is a single query that returns 3 rows.