【发布时间】:2016-11-20 10:09:02
【问题描述】:
我有一个名为Index 的表,其中包含id 和value 列,其中id 是一个自动递增的bigint,value 是一个带有英文单词的varchar。
我有一个名为Search 的表,它与表Index 有关系。对于每个搜索,您可以定义它应该在名为 Article 的表中搜索哪些索引。
表Article也与表Index有关系。
定义关系的表是:
Searches_Indexes 与列 id_search 和 id_index。
Articles_Indexes 与列 id_article 和 id_index。
我想查找所有包含相同搜索索引的文章。
例如:我有一个Search,索引为laptop 和dell,我想检索所有包含两个索引的Articles,而不仅仅是一个。
到目前为止,我有这个:
SELECT ai.id_article
FROM articles_indexes AS ai
INNER JOIN searches_indexes AS si
ON si.id_index = ai.id_index
WHERE si.id_search = 1
如何让我的 SQL 只返回 Articles 以及 Search 的所有 Indexes?
编辑:
样本数据:
文章:
id | name | description | ...
1 | 'Dell Laptop' | 'New Dell Laptop...' | ...
2 | 'HP Laptop' | 'Unused HP Laptop...' | ...
...
搜索:
id | name | id_user | ...
1 | 'Dell Laptop Search' | 5 | ...
索引:
id | value
1 | 'dell'
2 | 'laptop'
3 | 'hp'
4 | 'new'
5 | 'unused'
...
Articles_Indexes:
Article 和 id 1(戴尔笔记本电脑)具有 Indexes 'dell'、'laptop'、'new'。
Article 和 id 2(hp 笔记本电脑)具有 Indexes 'laptop'、'hp'、'unused'。
id_article | id_index
1 | 1
1 | 2
1 | 4
...
2 | 2
2 | 3
2 | 5
...
Searches_Indexes:
Search 和 id 1 仅包含 2 个 Indexes、“戴尔”和“笔记本电脑”:
id_search | id_index
1 | 1
1 | 2
所需输出:
id_article
1
【问题讨论】:
-
表称为搜索和索引...您还有哪些其他表 - 列、位置、选择和值?
-
已更新以使其更加清晰。我有两个关系表
Searches_Indexes和Articles_Indexes,它们基本上定义了哪些搜索有哪些索引,哪些文章有哪些索引。 -
edit 您的问题并添加一些示例数据和基于该数据的预期输出。 Formatted 文本 请no screen shots
-
@a_horse_with_no_name 我已经用示例数据更新了我的问题。
-
顺便说一句Full Text Search
标签: sql postgresql search indexing