繁体   English   中英

如何选择表a中具有表b中给出的n个特征的所有行

[英]How can I select all rows in a table a, which have n characteristics given in a table b

我正在制作一个用于出租房屋的网页。

出版物存储在这样的表中

ta_publications
+---+-------------+------+
|id |    name     | date |
+---+-------------+------+
| 1 | name_001    |  ... |
| 2 | name_002    |  ... |
| 3 | name_003    |  ... |
+---+-------------+------+

我有不同的出版物,它们具有“卫星电视”,“洗衣房清洁”等“功能”。

这些功能将来可能会更改,我希望能够添加/删除/修改它们,因此我将它们存储在表中的数据库中。

ta_feature_types
+---+-------------+
|id | name        |
+---+-------------+
| 1 | Internet    |
| 2 | Wi-Fi       |
| 3 | satelital tv|
+---+-------------+

与使用表格的出版物相关的

ta_features
+---+-------------+----------------+
|id |   type_id   | publication_id |
+---+-------------+----------------+
| 1 |      1      |       1        |
| 2 |      2      |       1        |
| 3 |      3      |       1        |
+---+-------------+----------------+

我认为这很容易理解; 有一个名为name_001的出版物,其中有互联网,无线网络和卫星电视。

我的问题是:我需要能够有效地搜索和选择具有某些功能的所有出版物(房屋)。 例如,所有具有Internet,WiFi和“可带宠物”功能的出版物。

我只是想出另一个问题:当用户喜欢一个出版物时,说“ house_003”,我如何获得它所具有的功能的列表?

如果要按功能名称获取出版物:

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

如果您已经知道功能ID

SELECT p.*
FROM ta_publications p
JOIN ta_features f ON f.publication_id = p.id
WHERE f.type_id = ? -- feature ID

编辑:要获取与所有多个功能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

要通过发布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

有关架构的一些说明:

  • 如上所述,除非发布可以具有相同的功能多次,例如“ 2x Wi-Fi”,否则您不需要ta_features表中的ID列。

  • 您的表名令人困惑,我可以建议您重命名

    • ta_featuresta_publication_features (或ta_pub_features )和
    • ta_feature_typesta_features
  • 出于性能原因,您应该在上述JOIN条件中使用的所有列上创建索引(在此处使用原始表名):

    • ta_publications(id)
    • ta_features(type_id, publication_id)
    • ta_feature_types(id)

如果用户选择了多个功能,请使用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)

您可以使用您选择的服务器语言通过循环生成以上内容。 即。

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

我认为您想要的是一个子查询:

select a.* 
from ta_publications as a
where '1' in (select type_id from ta_features where publication_id=a.id)

用“ 1”代替您想要的任何其他功能编号。

对于你的第二个问题。 一个简单的查询应该做到这一点:

select type_id 
from ta_features
where publication_id=[[[id that someone likes]]]

暂无
暂无

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

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