alpho07
alpho07

Reputation: 13

Re-write a nested mysql query so as to create a view

I have a nested query. Any suggestion on how i can rewrite the nested query so that I am able to create a view with it

Here is the query

select * 
from (select * from tracking_table order by id desc) x
group by labref);

Upvotes: 0

Views: 27

Answers (1)

Barmar
Barmar

Reputation: 780939

There doesn't seem to be any need for the nested query. It can just be:

SELECT *
FROM tracking_table
GROUP BY labref
ORDER BY id DESC

But both queries probably don't do what you want. All the columns will be chosen from unpredictable rows in each group, and there's no guarantee that different columns will come from the same row. You're also not guaranteed to get the highest ID in each group.

If you want the row with the highest ID for each labref, see SQL select only rows with max value on a column

Upvotes: 2

Related Questions