How to extract data from the table according to the value of the column using PHP and MySQL

advertisements

I need one help. I need to filter value from table as per column value using PHP and Mysql. I am explaining my table and code below.

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

I am explaining my query below.

$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 one record like below.

image_id     member_id      subcat_id   from_day   to_day

    3           220             56         1          1

Here I need the to_day should always be higher value. 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. 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

Please help me to resolve this issue.


It seems you have syntax problems, because if you copy-paste, you put "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.