基于同义词的MySQL高级搜索

基于同义词的MySQL高级搜索,mysql,Mysql,我有三张表,下面是相关的细节 1. Primary words +----+---------+ | id | word | +----+---------+ | 1 | Machine | +----+---------+ | 2 | phone | +----+---------+ 2. Alternative words +----+------------+-----------+ | id | primary_id | word | +----+-

我有三张表,下面是相关的细节

1. Primary words

+----+---------+
| id | word    |
+----+---------+
| 1  | Machine |
+----+---------+
| 2  | phone   |
+----+---------+

2. Alternative words    

+----+------------+-----------+
| id | primary_id | word      |
+----+------------+-----------+
| 1  | 1          | system    |
+----+------------+-----------+
| 2  | 1          | feature   |
+----+------------+-----------+
| 3  | 2          | telephone |
+----+------------+-----------+

3. product table

+----+------------------+
| id | name             |
+----+------------------+
| 1  | mobile system    |
+----+------------------+
| 2  | computer machine |
+----+------------------+
| 3  | wired telephone  |
+----+------------------+
现在扭曲的是,每当用户在产品表中使用“机器”搜索时,就会显示表名为“机器”或“系统”或“功能”的产品的结果,如果使用“系统”或“功能”搜索,则也会显示“机器”或“系统”或“功能”的结果。反之亦然


你能告诉我如何解决这个问题吗?

我建议把它放在一张表中,如下所示:

id | primary_id | word
1 | 1 | machine
2 | 2 | phone
3 | 1 | system
4 | 1 | feature
5 | 2 | telephone
查询如下,以
machine
word为例:

Select word from MyTable where primary_id in 
             (select primary_id from MyTable where word = "machine")
输出:
机器
系统
功能


更新

如果您有更多的列,例如为了显示这些单词的相关性,您可以再添加一个表来映射单词之间的关系,如下所示:

id | w1_id | w2_id | relation
1 | 1 | 3 | 80
2 | 2 | 5 | 90
3 | 1 | 4 | 60
4 | 3 | 4 | 60
单词表将只列出如下单词:

id| word
1 | machine
2 | phone
3 | system
4 | feature
5 | telephone

在中,您将看到一个长时间的讨论,讨论您必须如何设计模式的布局,以及如何检索数据的提示。

如果我正确理解您的要求

SQL

SELECT * 
  FROM Product P
 WHERE EXISTS(SELECT fw.Word  --(6) select all products that exist in this query
                FROM (SELECT pw.Word --(1) select all Primary words matching input
                        FROM PrimaryWords pw
                       WHERE pw.Word = 'machine'
                      UNION --(3) union the results from both selects
                      SELECT aw.Word --(2) select all Alternative words that match input or have its primary matching it
                        FROM PrimaryWords pw INNER JOIN AlternativeWords aw
                          ON pw.Id = aw.PrimaryId
                       WHERE pw.Word = 'machine'
                          OR aw.Word = 'machine') as fw --(4) alias the result
                WHERE p.Name LIKE '%' || fw.Word || '%'); -- (5) filter products that match the valid words

您可以阅读()中按编号排序的注释。

首先,您需要将这两个表数据取为一,然后给出条件

Select word from(
SELECT id,word FROM PrimaryWords
Union all
Select primaryid,word from AlternativeWords a
) where id in ( select id from primarywords where word='yoursearchketword'
Union
Select primaryid from AlternativeWords where word='yoursearchketword')
根据您的产品表更新答案

现在需要交叉连接product表,因为它们之间没有关系

还有一件事,您必须在这里使用like运算符将所需结果与prouct表的name列进行比较。在这里,我给出了如何实现这一点的小想法,但您可以轻松地对其进行改进

Select a.word,b.name from
  (Select word from(
    SELECT id,word FROM PrimaryWords
    Union all
    Select primaryid,word from AlternativeWords a
    ) where id in ( select id from primarywords where word='yoursearchketword'
    Union
    Select primaryid from AlternativeWords where word='yoursearchketword')) a, product b
Where a.word LIKE CONCAT('%',name, '%');


这将有助于显示您正在使用的查询。另外,需要明确的是:在您拥有的任何系统中,用户输入“machine”,执行搜索(前面提到的数据库查询运行),它返回3行“machine”、“system”、“feature”?使用第一个表中的
id
和第二个表中的
primary\u id
?请向我们展示您的尝试,因为目前这只是一个代码请求。用新的要求更新了我的答案。。。看看吧,这与桌子的外观无关,谢谢你的建议。但是我认为你的查询对我没有多大帮助,因为它只返回机器记录;你可以用任何东西替换word
machine
。不需要在你提到的表之间建立任何关系。我回答了你的第一个问题,而不是最后一个更新的问题!!谢谢,沙漠之狐,太棒了。但现在我有了产品表,所以需要将这个表与产品名称匹配。这意味着无论何时用户搜索机器,都需要显示与机器、系统和功能相关的产品,这是我在问题中提到的。那么,现在有了产品表了吗?伙计,你应该阅读你的问题并适当地更新它。是的,它更新了。请检查它,让我们知道你是否有任何想法?谢谢你的查询,我已经建立了第一个所有的话,然后与产品名称匹配。但还有一个问题,我已经完成了两个查询,如果您对单个查询有想法的话,那就太好了。您能为这个场景设置产品表吗“twist”是指每当用户在产品表中使用“machine”进行搜索时,都会显示表名为“machine”或“system”或“feature”的产品的结果,如果使用“machine”或“system”或“feature”进行搜索“系统”或“功能”,然后还显示“机器”或“系统”或“功能”的结果。反之亦然。“除了这两个表之外,您还有一个表吗?”在产品表中,它将在哪一列搜索“机器”字这两个表与产品表之间的关系是什么,如果知道这两个表和product表之间很容易写quertno关系,则需要匹配我们在这两个表中找到的单词,并仅基于这些单词获得产品。