views:

141

answers:

3

I want to select distinct product_series, but I want all the other columns too.

This is my query as it stands:

SELECT DISTINCT product_series 
FROM cart_product 
WHERE product_brand = "everlon" 
AND product_type = "ring" 
AND product_available = "yes"

But this only gives me product_series, but I need all the other columns in that row too. But if I try to select more than just product_series, I end up getting multiples of product series.

So basically what I want is * all the fields but I want to limit it so i only get 1 row per product series.

I am not sure if I am explaining this correctly so let me give an example:

if I have

product_series   product_id
----------------------------
"seriesA"        230
"seriesA"        231
"seriesB"        232
"seriesB"        233

I would get all the columns but only 1 per product_series:

product_series   product_id
----------------------------
"seriesA"        230
"seriesB"        232

How can I do that?

+1  A: 

Sounds like you want GROUP BY instead.

Jonathan Sampson
Thanks, changed my select to * and then added group by product_series at the end. That seems to have done the trick. Thanks! and merry christmas!
John Isaacks
Merry Christmas to you too, Mr. Isaacks! Great work here.
Jonathan Sampson
Note that the fact that results returned by a `GROUP BY` with hidden columns belong to the same row is not guaranteed. This is so with current implementation but can change any time in the future.
Quassnoi
+2  A: 

You can use GROUP BY to do that. But please realize that there has to be some way of "flattening" the group of multiple product_id's int a single value. This is achieved with an aggregate function, like MIN, MAX, or GROUP_CONCAT():

SELECT product_series, MAX(product_id) max_product_id
FROM cart_product 
WHERE product_brand = 'everlon'
AND product_type = 'ring'
AND product_available = 'yes'
GROUP BY product_series

BTW: please don't use double quotes in SQL to delimit strings. In all RDBMS-es but MySQL the double quote can be used to delimit identifiers, and only single quotes can be used to delimit string literals.

Roland Bouman
The example output lists the minimum product_id, not the max
OMG Ponies
+1. Not the ideal solution to the question, but a great expression of how to use GROUP BY.
James
@OMG ponies: perhaps, but the OP never said they cared which particular product they wanted to see. To me, mentioning MIN in the text seems enough to get started thinking about what you really want.
Roland Bouman
+3  A: 
SELECT  pi.*
FROM    (
        SELECT  DISTINCT product_series 
        FROM    cart_product 
        ) pd
JOIN    cart_product  pi
ON      pi.id =
        (
        SELECT  id
        FROM    cart_product po
        WHERE   product_brand = "everlon" 
                AND product_type = "ring" 
                AND product_available = "yes"
                AND po.product_series = pd.product_series
        LIMIT 1
        )

This will pick one product per series in no particular order.

Add an ORDER BY condition into the subquery to define the order.

You may also want to read this article in my blog:

Quassnoi
+1 great answer!
James
Thanks, I ended up needing it to also be the one with the lowest price, so I could show a starting at price for each series, so being able to order by was good thanks.
John Isaacks