如何获取属于国家/地区79和作者80以及出版商91
的图书的IDSELECT id FROM books WHERE (
(filter = 'country' AND cat = 79)
AND
(filter = 'author' AND cat = 80)
AND
(filter = 'publisher' AND cat = 91)
)
这是我的表结构
+----+----------+-----+
| ID | FITER | CAT |
+----+----------+-----+
| 1 | country | 79 |
| 1 | author | 80 |
| 1 | publisher| 91 |
| 2 | country | 476 |
| 2 | author | 85 |
| 2 | publisher| 121 |
+----+----------+-----+
谢谢
答案 0 :(得分:2)
这是这种表结构的问题,你真的有那么多的过滤器,它们不能自己成为列吗?无论如何,它会是这样的:
SELECT DISTINCT id
FROM books b
WHERE filter = 'country' AND cat = 79
AND EXISTS( SELECT 1 FROM books
WHERE filter = 'author' AND cat = 80
AND id = b.id)
AND EXISTS( SELECT 1 FROM books
WHERE filter = 'publisher' AND cat = 91
AND id = b.id);