Using same column multiple times in WHERE clause

前端 未结 8 763
醉话见心
醉话见心 2020-12-15 10:19

I have a following table structure.

USERS

PROPERTY_VALUE

PROPERTY_NAME

USER_

8条回答
  •  独厮守ぢ
    2020-12-15 10:37

    Assuming you want to select all the fields in the USERS table

    SELECT u.* 
    FROM USERS u
    INNER JOIN 
    (
        SELECT USERS.id as user_id, COUNT(*) as matching_property_count
        FROM USERS
        INNER JOIN (
            SELECT m.user_id, n.name as property_name, v.value
            FROM PROPERTY_NAME n
            INNER JOIN PROPERTY_VALUE v ON n.id = v.property_name_id
            INNER JOIN USER_PROPERTY_MAP m ON m.property_value_id = v.property_value_id
            WHERE  (n.id = @property_id_1 AND v.value = @property_value_1) -- Property Condition 1
                OR (n.id = @property_id_2 AND v.value = @property_value_2) -- Property Condition 2
                OR (n.id = @property_id_3 AND v.value = @property_value_3) -- Property Condition 3
                OR (n.id = @property_id_N AND v.value = @property_value_N) -- Property Condition N
        ) USER_PROPERTIES ON USER_PROPERTIES.user_id = USERS.id
        GROUP BY USERS.id
        HAVING COUNT(*) = N     --N = the number of Property Condition in the WHERE clause
        -- Note : 
        -- Use HAVING COUNT(*) = N if property matches will be "MUST MATCH ALL"
        -- Use HAVING COUNT(*) > 0 if property matches will be "MUST MATCH AT LEAST ONE"
    ) USER_MATCHING_PROPERTY_COUNT ON u.id = USER_MATCHING_PROPERTY_COUNT.user_id
    

提交回复
热议问题