I have a db table that stores multiple ‘tags’ for a client.
client_code | tag_key | tag_value |
---|---|---|
ABC | price | high |
ABC | size | big |
XYZ | price | high |
XYZ | size | small |
I need to filter for clients by particular tags.
This is easy if I am searching for one type of tag. For example, all clients that have a high price point:
SELECT DISTINCT(client_code) FROM tb_client_tags WHERE (tag_key = 'price' AND tag_value = 'high')
How would I do this for multiple key value pairs?
For example, if I wanted to search by price and size.
The following works, but it seems quite inefficient!
SELECT client_code FROM
(
SELECT client_code, SUM(C) AS total FROM
(
SELECT DISTINCT(client_code), 1 AS C FROM tb_client_tags WHERE (tag_key = 'pricepoint' AND tag_value = 'medium')
UNION ALL
SELECT DISTINCT(client_code), 1 AS C FROM tb_client_tags WHERE (tag_key = 'client_size' AND tag_value = 'large')
) T
GROUP BY client_code
) T_1
WHERE total = 2;
Any tips would be most appreciated!
2
Answers
You can search clients that match either of the condition (
WHERE
) and then choose those that match all of them (HAVING
):Btw, in your sample code
DISTINCT
is not a function. It affects all columns.If you need to filter by two key-value pairs only then
if you need to filter by 2-3-4-more key-value pairs then use generic technique provided in @slaakso‘s answer.