I have the following query:
$imgDimensions_query = "SELECT MAX(imgWidth) maxWidth, MAX(imgHeight) maxHeight FROM (
SELECT imgHeight, imgWidth FROM primary_images WHERE imgId=$imgId
UNION
SELECT imgHeight, imgWidth FROM secondary_images WHERE primaryId=$imgId) as MaxHeight";
It’s working fantastic, but I would like to know how I can find the value of the column imgId, as well as the table name, for both the maxWidth and maxHeight values?
The reason I want this is I need to know if the maxWidth and maxHeight values belong to the same item in the database.
I’m wondering if this is possible by amending the current SQL query?
What would be perfect is if, along with querying the maxWidth and maxHeight values, a boolean could be set up to output true if both the maxWidth and maxHeight belong to the same entry (at least once).
I’m thinking, since the image data in primary_images is unique from the data in secondary_images (and vice versa), a boolean could be set up in each of the queries, and as long as one true exists, true is output. Does that make sense? Is that possible?
I have managed to put together a second query which uses the values of maxWidth and maxHeight from the first query to output the number of images in a specific set that hold both values. All I really care about is if there is or if there isn’t one or more images that meet the above requirement, so again, a boolean would be better than the total number. If you have an idea of how to amend the following to show a boolean instead of the number of results, let me know!
I have been reassured that with a maximum number of entries in both tables being under 1000, using two queries instead of one shouldn’t cause a hit to speed. If you think so as well, and if combining these queries into one is ridiculous, then let me know that as well.
The second query:
$haveDimensions_query = "SELECT sum(rows) AS total_rows FROM (
SELECT count(*) AS rows FROM primary_images WHERE imgId = $imgId and imgWidth = $maxImageWidth and imgHeight = $maxImageHeight
UNION ALL
SELECT count(*) AS rows FROM secondary_images WHERE primaryId = $imgId and imgWidth = $maxImageWidth and imgHeight = $maxImageHeight
) as union_table";
You could do this in two queries, it’s probably easier to read that way.
First query is what you have to get to get the max height and width.
You then can issue the second query which looks like:
SElECT primaryId FROM ( SELECT imgHeight, imgWidth, imgId AS primaryId FROM primary_images UNION SELECT imgHeight, imgWidth, primaryId FROM secondary_images ) as union_table WHERE imgWidth = [maxWidth] and imgHeight = [maxHeight];Where
[maxWidth]and[maxHeight]are the two values that you get from previous query. If they belong to the same image ID, you will have query result greater than zero, if not this query will have no result.If you need to know which id is belong to which table, you could create artificial column (e.g. source) and your query would become:
SElECT primaryId, source FROM ( SELECT imgHeight, imgWidth, imgId AS primaryId, 1 as source FROM primary_images UNION SELECT imgHeight, imgWidth, primaryId, 2 as source FROM secondary_images ) as union_table WHERE imgWidth = [maxWidth] and imgHeight = [maxHeight];Note that there is now artificial column called source. So if your result from query is
You know that
imgId4 fromprimary_imagesas well asprimaryId4,5 fromsecondary_imagesmatch with the max height and max width of the previous queryAnd finally, if you just want to know whether there is image that is matching or not, per our comments and discussion below, you could do:
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 = [maxWidth] and imgHeight = [maxHeight];Where
imgCountwill be zero if there is no matching image or greater than zero otherwise