dtmjqyfz21793 2011-09-04 17:39
浏览 99
已采纳

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?

  • 写回答

1条回答 默认 最新

  • dongmiao520892 2011-09-04 20:13
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能