At least one X but no Ys Query

前端 未结 4 1026
梦如初夏
梦如初夏 2021-01-06 11:23

I come across this pattern occasionally and I haven\'t found a terribly satisfactory way to solve it.

Say I have a employee table and an review

4条回答
  •  忘掉有多难
    2021-01-06 11:41

    It's possible. The particular syntax depends on how you store 'good' and 'bad' reviews.

    Suppose you had a classification column in review that had values 'good' and 'bad'.

    Then you could do:

    SELECT employee.*
    FROM employee
    JOIN review 
     ON employee.id=review.employee_id
    GROUP BY employee.id
    HAVING SUM(IF(classification='good',1,0))>0 -- count up #good reviews, > 0
       AND SUM(IF(classification='bad',1,0))=0  -- count up #bad  reviews, = 0.
    

提交回复
热议问题