[英]How to fetch data from table as per column value using PHP and MySQL
I need to filter value from table as per column value using PHP and MySQL.我需要使用 PHP 和 MySQL 根据列值过滤表中的值。 Here is my data:这是我的数据:
db_images: db_images:
image_id member_id subcat_id from_day to_day
1 220 56 1 3
2 220 56 1 3
3 220 56 1 1
4 120 22 1 5
5 120 22 2 4
Here is my query:这是我的查询:
$qry=mysqli_query($connect,"select * from db_iamges group by member_id,subcat_id order by image_id desc");
Using my query I am getting a record like below:使用我的查询,我得到如下记录:
image_id member_id subcat_id from_day to_day
3 220 56 1 1
I need the to_day
should always be higher value.我需要to_day
应该总是更高的价值。 If same member_id and subcat_id
is present the the to_day will be always higher value and the from_day will be always smaller value.如果存在相同的member_id and subcat_id
,则 to_day 将始终为较高值,而 from_day 将始终为较小值。 The expected output should like below.预期的输出应该如下所示。
image_id member_id subcat_id from_day to_day
1 220 56 1 3
4 120 22 1 5
It seems you have syntax problems, because if you copy-paste, you put "db_iamges".看来你有语法问题,因为如果你复制粘贴,你把“db_iamges”。 I made a table:我做了一个表:
mysql> select * from prueba1;
+----------+-----------+-----------+----------+--------+
| image_id | member_id | subcat_id | from_day | to_day |
+----------+-----------+-----------+----------+--------+
| 1 | 220 | 56 | 1 | 3 |
| 2 | 220 | 56 | 1 | 3 |
| 3 | 220 | 56 | 1 | 1 |
| 4 | 120 | 22 | 1 | 5 |
| 5 | 120 | 22 | 2 | 4 |
| 6 | 120 | 22 | 2 | 9 |
| 7 | 120 | 22 | 2 | 2 |
+----------+-----------+-----------+----------+--------+
7 rows in set (0.00 sec)
And:和:
mysql> select image_id, member_id, subcat_id, min(from_day), max(to_day) from prueba1 group by member_id, subcat_id order by image_id asc;
+----------+-----------+-----------+----------+-------------+
| image_id | member_id | subcat_id | from_day | max(to_day) |
+----------+-----------+-----------+----------+-------------+
| 1 | 220 | 56 | 1 | 3 |
| 4 | 120 | 22 | 1 | 9 |
+----------+-----------+-----------+----------+-------------+
2 rows in set (0.00 sec)
It is working这是工作
EDIT: Updated, as I didn't understand your main problem.编辑:更新,因为我不明白你的主要问题。
Select
T.*
From db_images t
Inner join (
Select member_id, subcat_id, max(to_day) to_day
db_images group by member_id,subcat_id
) t2 on t.member_id = t2.member_id
And t.Subcat_id = t2.subcat_id
And t.to_day = t2.to_day;
使用 max(column name) 获得更高的 value 。
select image_id, member_id,subcat_id,from_day, max(to_day) as to_day from from db_iamges group by member_id,subcat_id order by image_id desc
try this尝试这个
select t1.* from db_images t1 inner join(select max(member_id) as
mid,max(subcat_id) as cid,min(from_day) as fdy from db_images group
by member_id) t2 on t1.member_id = t2.mid and t2.cid = t1.subcat_id
and t2.fdy = t1.from_day group by member_id order by image_id asc;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.