Jump to content

How to get at least 1 distinct result


optikalefx

Recommended Posts

Hi, I'm usually pretty skilled at mysql, but this stupid simple problem i must be overlooking.

 

I have a store with a bunch of orders in it.  I want to get a result of the most recent purchases, but don't get duplicates.

 

So if the purchases are

p1, p2, p2, p2, p2, p3, p4

 

im trying to get

p1, p2, p3, p4 as my result

 

This

SELECT
products_name
FROM 
        orders_products
ORDER BY 
        orders_products_id desc

 

gives me all results

 

but this

SELECT
distinct products_name
FROM 
        orders_products
ORDER BY 
        orders_products_id desc

 

gives me totally different results. it gives me like p4, p6, p9p, p15.

group by gives me the same.

 

any ideas? i hope i explained that correctly.

Link to comment
https://forums.phpfreaks.com/topic/224450-how-to-get-at-least-1-distinct-result/
Share on other sites

There may be a shorter way but this should work:

 

SELECT products_name FROM orders_products WHERE orders_products_id IN(SELECT MAX(orders_products_id) FROM orders_products GROUP BY products_name);

 

(I tried the GROUP BY ORDER BY and it didn't work, same with DISTINCT.)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.