[英]How to fetch value from table keeping max,min column value using MySQL and PHP
我需要一个帮助。 我需要通过使用MySQL将其某些值保持为max或min来从表中获取所有记录。 我在下面解释我的表和代码。
db_images:
id member_id day1 day2 images
1 241 1 1 asc.png
2 241 1 2 xzc.png
3 241 2 3 ohjy.png
4 240 1 5 asd.png
这是我的桌子。我在下面解释我的查询。
$member_id=241
$qry=mysqli_query($connect,"select * from db_images where member_id='".$member_id."'");
在这里我可以根据条件从表中获得3条记录。 但是我需要在每个记录中保留min(day1) and max(day2)
值。 意味着每个记录将根据所需条件包含day1=1 and day2=3
,并且应提取相同的3条记录。 请帮我。
您可以尝试使用子选择。
select minday1, maxday2, t1.id, t2.member_id, t1.images
from (select min(day1) as minday1, max(day2) as maxday2, member_id from db_images
group by member_id) as t2, db_images as t1
where t2.member_id = t1.member_id and t1.member_id=$member_id
请尝试这个
SELECT * ,
(select MIN(`day1`) from `db_images` where member_id = ".$member_id.") as minDay ,
(select MAX(`day2`) from `db_images` where member_id= ".$member_id.") as maxDay
FROM `db_images` WHERE `member_id` = ".$member_id." ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.