Select records based on column priority

前端 未结 4 1149
Happy的楠姐
Happy的楠姐 2021-01-23 06:37

First of all, the title of this question is horrible, but I didn\'t find a better way to describe my issue.

There\'s probably a very easy way to do this, but I couldn\'t

4条回答
  •  北海茫月
    2021-01-23 07:20

    Your query is nearly correct. Just use PRODUCTID and not ID.

    SELECT * 
    FROM xx
    WHERE f_COLOUR = "GREEN"
    UNION
    SELECT * 
    FROM xx 
    WHERE PRODUCTID not in 
                    (SELECT PRODUCTID
                     FROM xx 
                     WHERE f_COLOUR = "GREEN");
    

    SQLFiddle Demo

提交回复
热议问题