假设我有两个表:
product
-------
productid
name
和
language
--------
productid
code
我有三种产品:
1, 'homebody' -> 1, EN
2, 'continetnal' -> 2, FR
-> 2, EN
-> 2, ES
3, 'westy' -> 3, EN
-> 3, ES
-> 3, FR
-> 3, PT
4, 'oktoberfest' -> 4, DE
假设我有一个我感兴趣的语言列表EN, FR, ES
,我想创建三种类型的查询来选择多个产品。
我认为第一种解决方案只是:
SELECT * FROM products AS p JOIN language AS l ON (p.productid = l.productid) WHERE l.code IN ('EN', 'ES', 'FR')
解决其他问题的好方法是什么?我怀疑我计算与我的产品相关的语言数量,并确保它与我的查询中的语言数相等或至少相同,但不确定如何实现这一点。
答案 0 :(得分:2)
如果您不能为同一产品提供两次FR(或任何语言):
2
SELECT p.productid, p.name
FROM products AS p
JOIN language AS l ON (p.productid = l.productid)
WHERE l.code IN ('EN', 'ES', 'FR')
GROUP BY p.productid, p.name
HAVING COUNT(*) = 3
3
WHERE l.code IN ('EN', 'ES', 'FR')
AND NOT EXISTS (SELECT NULL FROM language where productid = p.productid
AND code NOT IN ('EN', 'ES', 'FR')
GROUP BY p.productid, p.name
HAVING COUNT(*) = 3
或者如果您想检索语言代码
SELECT * FROM products p
JOIN language l on p.productid = l.productid
where p.productid IN
(SELECT p1.productid from product p1
JOIN language AS l1 ON (p1.productid = l1.productid)
WHERE l1.code IN ('EN', 'ES', 'FR')
GROUP BY p1.productid,
HAVING COUNT(*) >= 3)
答案 1 :(得分:-2)
以下解决方案如何?
匹配我的列表中的任何语言('homebody','continental','westy')
select p.* from product p, language l
where p.productid = l.productid
and l.code in ('homebody', 'continental', 'westy')
匹配我列表中的所有语言('continental','westy')
select distinct p.* from product p, product p2, language l
where p.productid = l.productid
and l.code in ('continental', 'westy')
and p.productid = p2.productid
完全匹配我的列表中的语言('大陆')
select p.*
from product p, language l
where p.productid = l.productid
and l.code in ('continental')