I have a table and I want to fetch the lastest row which type
= 3. based on the user_id also rest of the rows fetched which not type
=3
Table – galleries
+----+----------+------------+----------+
| id | user_id | type | photo |
+----+----------+------------+----------+
| 1 | 1 | 1 | img1.jpg |
| 2 | 2 | 1 | img2.jpg |
| 3 | 1 | 3 | img3.jpg |
| 4 | 1 | 2 | img4.jpg |
| 5 | 1 | 3 | img5.jpg |
| 6 | 3 | 1 | img6.jpg |
| 7 | 2 | 3 | img7.jpg |
+----+----------+------------+----------+
In above table, user_id=1 has more than 1 records which type=3. So in this case, I want to show the last record of this user using MySql Query.
My Output should be like:
+----+----------+------------+----------+
| id | user_id | type | photo |
+----+----------+------------+----------+
| 1 | 1 | 1 | img1.jpg |
| 2 | 2 | 1 | img2.jpg |
| 4 | 1 | 2 | img4.jpg |
| 5 | 1 | 3 | img5.jpg |
| 6 | 3 | 1 | img6.jpg |
| 7 | 2 | 3 | img7.jpg |
+----+----------+------------+----------+
Please let me know how can I prepare a MySQL query for this?
2
Answers
Assuming that "last record" means the one with the highest Id you can do something like this:
This query will filter only records that have type=3, group filtered records by user_id and for each group return only the record with the highest id. If this is the criteria to identify a record as last then the query should return what you are looking for
You can use
ROW_NUMBER()
to identify the rows that are duplicate, byPARTITION
-ing the row numbering according to your requirements, then exclude them by filtering on the row number. I’ve used a derived table (i.e.select from () x
), but you can achieve the same thing with a CTE or temp table.First we create a table for the sample data:
Then we select from it, rank and filter.
And we see the results:
Here’s a dbfiddle demonstrating it.
BTW you will need to be running MYSQL v8.0 or greater for the
ROW_NUMBER()
function to be supported. If not, here’s a stack overflow post discussing this change.