【发布时间】:2021-02-11 01:47:36
【问题描述】:
数据库
产品
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(128) | NO | UNI | NULL | |
+-------------+--------------+------+-----+---------+----------------+
标签
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(128) | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
products_tags
+------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| product_id | int(11) | NO | MUL | NULL | |
| tag_id | int(11) | NO | MUL | NULL | |
+------------+---------+------+-----+---------+----------------+
目标
返回被 2+ 个标签标记的 产品,例如“礼物”和“生日”。
它可能看起来像:
SELECT p.name FROM products p
LEFT JOIN products_tags p_t ON p_t.product_id = p.id
LEFT JOIN tags t ON t.id = p_t.tag_id
WHERE <what-is-the-condition?>
缺少可以通过同时存在和生日的标签选择的正确条件。
类似:
WHERE t.name = 'present' AND t.name = 'birthday';
【问题讨论】:
标签: mysql sql count where-clause having-clause