简体   繁体   English

用于获取列匹配(x AND y AND z)的所有行的查询或代码解决方案

[英]A query or code solution for getting all rows where a column matches (x AND y AND z)

Basically, we know that SQL IN is used to replace many OR conditions (x OR y OR z).基本上,我们知道SQL IN 是用来替换很多OR 条件(x OR y OR z)的。 But in this case I need a query, or a code solution to select all rows that contain (x AND y AND z).但在这种情况下,我需要一个查询或一个代码解决方案来选择包含 (x AND y AND z) 的所有行。 Something like http://www.mobile.de/ is using.http://www.mobile.de/这样的东西正在使用。 On their site, when searching for vehicles the more features you select the less results you get.在他们的网站上,搜索车辆时选择的功能越多,获得的结果就越少。 So they're obviously not using IN in their query.所以他们显然没有在查询中使用 IN 。 A really simplified version of my database would be this.我的数据库的一个真正简化的版本就是这个。

Adverts table:广告表:

--------------
| id | title |
--------------

Features table:特性表:

---------------
| id | f_name |
---------------

AdvertsFeatures table:广告功能表:

-------------------------------
| id | advert_id | feature_id |
-------------------------------

For the features search filter I do a LEFT JOIN with the AdvertsFeatures table and apply the following WHERE clause:对于功能搜索过滤器,我对 AdvertsFeatures 表执行 LEFT JOIN 并应用以下 WHERE 子句:

WHERE feature_id IN (x, y, z)

Thus, the Adverts that have any of the selected features come up.因此,具有任何选定特征的广告出现。 But like I said, I need a query or a code solution that gets only the Adverts that have all but not necessarily only the selected features.但就像我说的,我需要一个查询或一个代码解决方案,它只获取具有所有但不一定只有选定功能的广告。 A user can select any number of features from a total of around 40 features.用户可以从总共大约 40 个特征中选择任意数量的特征。 That would be a lot of WHERE clause "AND"s, which I already have many of.那将是很多 WHERE 子句“AND”,我已经有很多了。

How do I go about doing this?我该怎么做? It doesn't have to be SQL, it could be a code solution.它不一定是 SQL,它可以是代码解决方案。

This is an example of a "set-within-sets" query.这是“集合内集合”查询的示例。 I like to solve these using aggregation and the having clause.我喜欢使用聚合和having子句来解决这些问题。 In your case, you can do it this way:在你的情况下,你可以这样做:

select advert_id
from AdvertsFeatures af
where feature_id in (x, y, z)
group by advert_id
having count(*) = 3;

Note that you need to change the 3 to be the number of items in the in list.请注意,您需要将3更改为in列表中的项目数。 This also assumes that there are no duplicates in the AdvertsFeatures data (otherwise, you just use count(distinct featureid) in the having clause).这还假定有在没有重复AdvertsFeatures数据(否则,您只需使用count(distinct featureid)having条款)。

Gordon's query above can be used as an inline view to obtain the advert_id's. Gordon 上面的查询可以用作内联视图来获取 advert_id。 Then, you join these advert_id's with the AdvertFeatures table again to get all the adverts, as below:然后,您再次将这些 advert_id 与 AdvertFeatures 表连接以获取所有广告,如下所示:

SELECT af1.*
FROM AdvertsFeatures af1
INNER JOIN  
    (SELECT advert_id
    FROM AdvertsFeatures
    WHERE feature_id in (x, y, z)
    GROUP BY advert_id
    HAVING count(*) = 3;
) af2
ON af1.advert_id = af2.advert_id;

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

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