简体   繁体   English

使用SQL匹配特定的一对多条目

[英]Matching specific one-to-many entries with SQL

Suppose I have two tables: 假设我有两个表:

product
-------
productid
name

and

language
--------
productid
code

I have three products: 我有三种产品:

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

Suppose I have a list of languages I am interested EN, FR, ES in I would like to create three types of query to select a number of products. 假设我有一个我感兴趣的语言列表,我想用EN, FR, ES创建三种类型的查询来选择多种产品。

  1. Match any language in my list ('homebody', 'continental', 'westy') 匹配我列表中的任何语言(“房屋”,“大陆”,“西部”)
  2. Match all languages in my list ('continental', 'westy') 匹配我列表中的所有语言(“大陆”,“西方”)
  3. Match exactly languages in my list ('continental') 完全匹配我列表中的语言(“大陆”)

I think the solution to the first is just: 我认为第一种解决方案是:

SELECT * FROM products AS p JOIN language AS l ON (p.productid = l.productid) WHERE l.code IN ('EN', 'ES', 'FR')

What is a good way for me to solve the other queries? 对我来说,解决其他查询的好方法是什么? I suspect something where I count the number of languages associated with my product and make sure it is equal-to or at-least the number of languages in my query, but not sure how to make this happen. 我怀疑某些地方会计算与我的产品关联的语言数量,并确保它等于或至少等于查询中的语言数量,但不确定如何实现此目的。

if you can't have two times FR (or any language) for the same product : 如果同一商品不能有两倍的FR(或任何语言):

2. 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. 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

or if you wanna retrieve languages codes 或者如果您想检索语言代码

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)

How about the following solutions? 以下解决方案如何?

  1. Match any language in my list ('homebody', 'continental', 'westy') 匹配我列表中的任何语言(“房屋”,“大陆”,“西部”)

     select p.* from product p, language l where p.productid = l.productid and l.code in ('homebody', 'continental', 'westy') 
  2. Match all languages in my list ('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 
  3. Match exactly languages in my list ('continental') 完全匹配我列表中的语言(“大陆”)

     select p.* from product p, language l where p.productid = l.productid and l.code in ('continental') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM