[英]How can I select all rows in a table a, which have n characteristics given in a table b
I'm making a web page for renting houses. 我正在制作一个用于出租房屋的网页。
the publications are stored in a table like this 出版物存储在这样的表中
ta_publications
+---+-------------+------+
|id | name | date |
+---+-------------+------+
| 1 | name_001 | ... |
| 2 | name_002 | ... |
| 3 | name_003 | ... |
+---+-------------+------+
I have diferent publications, which have "features" such as "satellite tv", "Laundry cleaning", etc. 我有不同的出版物,它们具有“卫星电视”,“洗衣房清洁”等“功能”。
These features might change in the future, and I want to be able to add/remove/modify them, so I store them in the database in a table. 这些功能将来可能会更改,我希望能够添加/删除/修改它们,因此我将它们存储在表中的数据库中。
ta_feature_types
+---+-------------+
|id | name |
+---+-------------+
| 1 | Internet |
| 2 | Wi-Fi |
| 3 | satelital tv|
+---+-------------+
which are related to the publications using a table 与使用表格的出版物相关的
ta_features
+---+-------------+----------------+
|id | type_id | publication_id |
+---+-------------+----------------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 3 | 1 |
+---+-------------+----------------+
I think it's pretty easy to understand; 我认为这很容易理解; There is a publication called name_001 which have internet, wi-fi and satellite tv. 有一个名为name_001的出版物,其中有互联网,无线网络和卫星电视。
My problem is: I need to be able to efficiently search and select all publications(houses) wich have certain features. 我的问题是:我需要能够有效地搜索和选择具有某些功能的所有出版物(房屋)。 For example, all publications that have internet, wifi and "pets-allowed" features. 例如,所有具有Internet,WiFi和“可带宠物”功能的出版物。
I just came up with another question: When the user likes one publication, say "house_003", how do I get a list of the features that it does have? 我只是想出另一个问题:当用户喜欢一个出版物时,说“ house_003”,我如何获得它所具有的功能的列表?
If you want to get publications by feature name : 如果要按功能名称获取出版物:
SELECT p.*
FROM ta_publications p
JOIN ta_features f ON f.publication_id = p.id
JOIN ta_feature_types t ON f.type_id = t.id
WHERE t.name = ? -- feature name
If you already know the feature ID : 如果您已经知道功能ID :
SELECT p.*
FROM ta_publications p
JOIN ta_features f ON f.publication_id = p.id
WHERE f.type_id = ? -- feature ID
EDIT: To get all publications that match all of multiple feature IDs: 编辑:要获取与所有多个功能ID匹配的所有出版物:
SELECT p.id, p.name
FROM pub p
JOIN pub_feat pf ON pf.pub_id = p.id
WHERE pf.feat_id IN ? -- list of feature IDs, e.g. (1,2,3)
GROUP BY p.id, p.name HAVING COUNT(*) = ? -- size of list of feature IDs, e.g. 3
To get all the features (names, I assume) by publication ID: 要通过发布ID获取所有功能(我假设为名称):
SELECT t.name
FROM ta_feature_types t
JOIN ta_features f ON f.type_id = t.id
JOIN ta_publications p ON f.publication_id = p.id
WHERE p.id = ? -- publication ID
Some notes on your schema: 有关架构的一些说明:
As I commented above, you don't need an ID column in the ta_features
table unless a publication can have the same features multiples times, eg "2x Wi-Fi" 如上所述,除非发布可以具有相同的功能多次,例如“ 2x Wi-Fi”,否则您不需要ta_features
表中的ID列。
Your table names are confusing, may I suggest you rename 您的表名令人困惑,我可以建议您重命名
ta_features
to ta_publication_features
(or ta_pub_features
) and ta_features
到ta_publication_features
(或ta_pub_features
)和 ta_feature_types
to ta_features
ta_feature_types
到ta_features
For performance reasons you should create indices on all the columns used in the above JOIN conditions (using your original table names here): 出于性能原因,您应该在上述JOIN条件中使用的所有列上创建索引(在此处使用原始表名):
ta_publications(id)
ta_features(type_id, publication_id)
ta_feature_types(id)
If the user selects multiple features use the IN
keyword and a list of all features for a publication: 如果用户选择了多个功能,请使用IN
关键字和发布的所有功能的列表:
SELECT p.*
FROM ta_publications p
WHERE '1' in (select type_id from ta_features where publication_id = p.id)
AND '2' in (select type_id from ta_features where publication_id = p.id)
AND '3' in (select type_id from ta_features where publication_id = p.id)
You could generate the above with a loop in your server language of choice. 您可以使用您选择的服务器语言通过循环生成以上内容。 ie. 即。
SELECT p.*
FROM ta_publications p
WHERE 1=1
//START SERVER LANGUAGE
for (feature in featuresArray){
print("AND '$feature' in (select type_id from ta_features where publication_id = p.id)");
}
//END SERVER LANGUAGE
I think what you want is a subquery: 我认为您想要的是一个子查询:
select a.*
from ta_publications as a
where '1' in (select type_id from ta_features where publication_id=a.id)
Substitute '1' for any other feature number you want. 用“ 1”代替您想要的任何其他功能编号。
For your second question. 对于你的第二个问题。 A simple query should do it: 一个简单的查询应该做到这一点:
select type_id
from ta_features
where publication_id=[[[id that someone likes]]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.