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
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.)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.