Can you solve this simple SQL query?

后端 未结 3 828
伪装坚强ぢ
伪装坚强ぢ 2021-01-14 20:31

Suppose it\'s a website that sells photo cameras. Here are my entities (tables):

Camera: A simple camera
Feature: A feature like: 6mp, max resolution 1024x76         


        
3条回答
  •  囚心锁ツ
    2021-01-14 20:50

    This is easiest to generalise by putting the search values into a table...

    INSERT INTO search SELECT 1
             UNION ALL SELECT 2
             UNION ALL SELECT 3
    
    SELECT
      camera_features.camera_id
    FROM
      camera_features
    INNER JOIN
      search
        ON search.id = camera_features.feature_id
    GROUP BY
      camera_features.camera_id
    HAVING
      COUNT(DISTINCT camera_features.feature_id) = (SELECT COUNT(DISTINCT id) FROM search)
    

提交回复
热议问题