Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6589347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:09:44+00:00 2026-05-25T17:09:44+00:00

I have the following query: $imgDimensions_query = SELECT MAX(imgWidth) maxWidth, MAX(imgHeight) maxHeight FROM (

  • 0

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";
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:09:44+00:00Added an answer on May 25, 2026 at 5:09 pm

    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

    primaryId     source
    4             1 
    4             2
    5             2
    

    You know that imgId 4 from primary_images as well as
    primaryId 4,5 from secondary_images match with the max height and max width of the previous query

    And 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 imgCount will be zero if there is no matching image or greater than zero otherwise

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following MySQL query: $imgDimensions_query = SELECT imgWidth, imgHeight FROM ( SELECT
I have following sql query for finding 2nd max salary. Select * From Employee
I have the following query: select column_name, count(column_name) from table group by column_name having
I have the following query: SELECT c.* FROM companies AS c JOIN users AS
I have the following query: SELECT rowid FROM table1 ORDER BY RANDOM() LIMIT 1
I have the following query SELECT e.topicShortName, d.catalogFileID, e.topicID FROM catalog_topics a LEFT JOIN
I have the following query: SELECT S.[FlowOrder], S.[DESCRIPTION], COUNT(I.ID) FROM WorkFlowStatus AS S INNER
I have the following query: SELECT title, karma, DATE(date_uploaded) as d FROM image ORDER
i have the following query: select * from table where table.DateUpdated >='2010-05-03 08:31:13:000' all
I have following query in PostgreSQL: SELECT COUNT(a.log_id) AS overall_count FROM Log as a,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.