[英]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.