开发者

PHP: Find largest value from results of two MySQL database queries

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜