【问题标题】:Advance searching in MySQL according to synonym words根据同义词在MySQL中进行高级搜索
【发布时间】:2018-10-09 12:59:27
【问题描述】:

我有三个表,下面是相关的详细信息

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  |
+----+------------------+

现在的转折是,每当用户在产品表中使用“机器”搜索时,会显示表名称为“机器”或“系统”或“功能”的产品结果,如果使用“系统”或“功能”搜索,那么也显示“机器”或“系统”或“功能”的结果。反之亦然。

您能建议我如何解决这个问题吗?

【问题讨论】:

  • 这将有助于显示您正在使用的查询。另外,要明确一点:在您拥有的任何系统中,用户键入“机器”,执行搜索(上述数据库查询运行),它返回 3 行“机器”、“系统”、“功能”?跨度>
  • 使用第一个表中的id 和第二个表中的primary_id
  • 请向我们展示您的尝试,因为目前这只是一个代码请求。
  • 用新要求更新了我的答案...检查一下

标签: mysql


【解决方案1】:

如果我理解正确你在问什么......

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

您可以读取按 () 中编号排序的 cmets。

【讨论】:

  • 谢谢,@DesertFox 太好了。但现在扭曲的是我有产品表,所以需要将这个与产品名称相匹配。意味着每当用户搜索机器时,都需要显示与机器、系统和功能相关的产品,这是我在问题中提到的。
  • 那么,现在有产品表了吗?伙计,你应该阅读你的问题并适当地更新它。
  • 是的,它已更新。请检查一下,如果您有任何想法,请告诉我们?
  • 感谢您的查询,我首先创建了所有单词,然后与产品名称匹配。但是我已经完成了两个查询,如果您对单个查询有一个想法,那就太好了。
【解决方案2】:

首先你需要将这两个表数据合并为一个,然后给出条件。

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')

根据您的产品表更新答案

现在你需要交叉连接产品表,因为它们之间没有任何关系。

还有一件事是您必须在此处使用like 运算符将您想要的结果与产品表的名称列进行比较。在这里,我给出了如何实现的小想法,但您可以轻松地改进它。

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, '%');

【讨论】:

  • 你能在这个场景中用产品表设置这个吗?扭曲的是,每当用户在产品表中搜索“机器”时,就会显示表名有“机器”或“系统”的产品的结果或“功能”,如果使用“系统”或“功能”搜索,则还会显示“机器”或“系统”或“功能”的结果。反之亦然。”
  • 除了这两张桌子,你还有一张桌子吗??
  • 在产品表的哪一列中搜索“机器”字样
  • 这2个表和product表有什么关系,知道的话就好写quert
  • 与这2个表和product表没有关系,需要匹配我们在2个表中找到的单词并仅根据这些单词获取产品。
【解决方案3】:

我建议把它放在一个这样的表中:

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

查询是这样的,我们以machine这个词为例:

Select word from MyTable where primary_id in 
             (select primary_id from MyTable where word = "machine")

输出:machinesystemfeature


更新

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

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

this thread 中,您会发现一个很长的讨论,讨论您必须设计架构布局的方式以及如何检索数据的提示。

【讨论】:

  • 看表没关系,谢谢建议。但我认为您的查询对我没有帮助,它只会返回机器记录。
  • 你可以随心所欲地改变它;你可以用任何东西替换单词machine
  • 不需要在你提到的表之间做任何关系。
  • 我回答了你的第一个问题,而不是最后一个更新的问题!!
猜你喜欢
  • 2013-02-14
  • 2011-12-25
  • 2017-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多