PHP:从两个MySQL数据库查询的结果中查找最大值

PHP:从两个MySQL数据库查询的结果中查找最大值

问题描述:

I have the following two mysql_queries:

1.

$primary_img_query = "SELECT imgWidth, imgHeight FROM primary_images WHERE imgId=$imgId";
$primary_img_data = mysql_query($primary_img_query) or die('MySql Error' . mysql_error());

2.

$secondary_img_query = "SELECT imgWidth, imgHeight FROM secondary_images WHERE primaryId=$imgId";
$secondary_img_data = mysql_query($secondary_img_query) or die('MySql Error' . mysql_error());

What I need to do is find the largest value of both imgWidth and imgHeight from each query, and then find the largest value between the two found values. I need both said largest values to end up in variables.

All values in imgWidth and imgHeight are positive integers greater than zero.

Thanks for any help you can provide.


I was thinking I could put the results from both imgWidth and imgHeight in each query in separate arrays, then combine the arrays, and use max() to find the largest(highest) value. Would that work?

我有以下两个 mysql_queries code>: strong> p >

1。 strong> p>

  $ primary_img_query =“SELECT imgWidth,imgHeight FROM primary_images WHERE imgId = $ imgId”; 
  $ primary_img_data = mysql_query($ primary_img_query)或die('MySql Error'.mysql_error()); 
  code>  pre> 
 
 

2。 strong> p >

  $ secondary_img_query =“SELECT imgWidth,imgHeight FROM secondary_images WHERE primaryId = $ imgId”; 
 $ secondary_img_data = mysql_query($ secondary_img_query)或die('MySql Error'.mysql_error())  ; 
  code>  pre> 
 
 

我需要做的是从每个查询中找到 imgWidth code>和 imgHeight code>的最大值 ,然后找到两个找到的值之间的最大值。 我需要两个所说的最大值来结束变量。 p>

imgWidth code>和 imgHeight code>中的所有值都是大于零的正整数。 p>

感谢你提供的任何帮助。 p>


我以为我可以把结果都来自 imgWidth code>和 imgHeight code>在单独数组中的每个查询中,然后组合数组,并使用 max() code>来查找最大(最高)值。 那会有用吗? p> div>

This query should give you MAX for the width and height given the two need not to be associated with anything. You could also filter the set. For example, by primaryId below, assuming the primaryId is a number, you want to filter for primaryId less than 2

SELECT MAX(imgWidth) maxWidth, MAX(imgHeight) maxHeight FROM (
    SELECT imgHeight, imgWidth, primaryId FROM primary_images
    UNION 
    SELECT imgHeight, imgWidth, primaryId FROM secondary_images
  ) as MaxHeight
WHERE primaryId < 2

You would then store the results as variables then use it to size your container.