How do I (or can I) SELECT DISTINCT on multiple columns?

前端 未结 5 674
梦谈多话
梦谈多话 2020-11-21 23:53

I need to retrieve all rows from a table where 2 columns combined are all different. So I want all the sales that do not have any other sales that happened on the same day f

5条回答
  •  再見小時候
    2020-11-22 00:05

    SELECT DISTINCT a,b,c FROM t
    

    is roughly equivalent to:

    SELECT a,b,c FROM t GROUP BY a,b,c
    

    It's a good idea to get used to the GROUP BY syntax, as it's more powerful.

    For your query, I'd do it like this:

    UPDATE sales
    SET status='ACTIVE'
    WHERE id IN
    (
        SELECT id
        FROM sales S
        INNER JOIN
        (
            SELECT saleprice, saledate
            FROM sales
            GROUP BY saleprice, saledate
            HAVING COUNT(*) = 1 
        ) T
        ON S.saleprice=T.saleprice AND s.saledate=T.saledate
     )
    

提交回复
热议问题