Answer the question
In order to leave comments, you need to log in
How to select rows in sql that contain several tags at once?
Guys how to select rows in sql that contain several tags at once?
We have two tables:
FruitsTABLE
id name
1 яблоко
2 банан
3 апельсин
FruitTagTABLE
fruit tag
1 сладкий
1 круглый
2 сладкий
3 сладкий
3 круглый
SELECT DISTINCT fruitsTABLE.* FROM FruitsTABLE JOIN FruitTagTABLE ON FruitsTABLE.id = FruitTagTABLE.id WHERE FruitTagTABLE.tag = 'сладкий' OR FruitTagTABLE.tag = 'круглый'
Answer the question
In order to leave comments, you need to log in
SELECT `f`.*
FROM `FruitsTABLE` AS `f`
JOIN `FruitTagTABLE` AS `ft1` ON `ft1`.`tag` = :tag1 AND `ft1`.`id` = `f`.`id`
JOIN `FruitTagTABLE` AS `ft2` ON `ft2`.`tag` = :tag2 AND `ft1`.`id` = `f`.`id`
select *
from FruitsTABLE f
where name = 'яблоко'
and exists (select *
from FruitTagTABLE t
where t.fruit = f.fruit
and f.tag = 'сладкий'
)
and exists (select *
from FruitTagTABLE t
where t.fruit = f.fruit
and f.tag = 'круглый'
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question