使用 mySQL SELECT 的结果作为 MATCH AGAINST 自然语言查询的输入

问题描述

我需要通过自然语言对 MysqL 表字段进行研究,将另一个表字段的值作为输入。我尝试了类似的东西,但正如我所怀疑的那样,这是不正确的:

    SELECT id,name,MATCH(name),AGAINST 
      (
        SELECT name
        FROM table2
      ) AS score
    FROM table1
    WHERE MATCH(name),AGAINST 
      (
        SELECT name
        FROM table2
      )

有什么想法吗?

更新 1

我按照下面的示例进行操作,但出现错误“#1064 - 'tb2 附近的 sql 查询语法错误”。我看不到这个语法错误。这是我正在测试的代码

SELECT name,MATCH(name) AGAINST 
(

    (SELECT name
    FROM 
    (
        SELECT name 
            FROM active_ingredients

        UNION ALL
        
        SELECT active_ingredients.name as name
        FROM active_ingredients
        INNER JOIN temp_active_ingredients_aliases ON temp_active_ingredients_aliases.alias_name = active_ingredients.name

    ) tbl
    GROUP BY name
    HAVING count(*) = 1
    ORDER BY name) tb2 

) AS score
FROM 
(
    
    SELECT alias_name as name
    FROM temp_active_ingredients_aliases
    


) 
WHERE MATCH(name) AGAINST 
(

    (SELECT name
    FROM 
    (
        SELECT name 
            FROM active_ingredients

        UNION ALL
        
        SELECT active_ingredients.name as name
        FROM active_ingredients
        INNER JOIN temp_active_ingredients_aliases ON temp_active_ingredients_aliases.alias_name = active_ingredients.name

    ) tb3
    GROUP BY name
    HAVING count(*) = 1
    ORDER BY name) tb4

)

内部查询返回不完全匹配 temp_active_ingredients_aliases.alias_name 字段之一的 active_ingredients.name 列表。这样我就可以通过自然语言 FULLTEXT 研究尝试将不完全匹配的名称与 alias_name(s) 进行匹配。需要注意的是,以下内部查询工作正常:

    SELECT name
    FROM 
    (
        SELECT name 
            FROM active_ingredients

        UNION ALL
        
        SELECT active_ingredients.name as name
        FROM active_ingredients
        INNER JOIN temp_active_ingredients_aliases ON temp_active_ingredients_aliases.alias_name = active_ingredients.name

    ) tbl
    GROUP BY name
    HAVING count(*) = 1
    ORDER BY name

我很确定语法错误非常愚蠢,但我看不到。

更新 2

这里是生成两个表(架构和一些数据)的代码链接

解决方法

Yiu 快到了

进一步解释一下。

  1. 您在 match(name) 后面添加了一个在该位置错误的逗号。
  2. GROUP_CONCAT 存在是因为 MySQL 需要一个单词列表,这是实现这一目标的最简单方法,有关 FulltextaSearch 和优化的更多信息,请参阅 manual 正如您在示例中看到的那样,空格作为分隔符不不改变任何东西
  3. IN NATURAL LANGUAGE MODE 只是我的假设,因为你写了自然语言研究所有其他选项也在该链接中解释
CREATE TABLE table1
(id int,name text,FULLTEXT(name))
INSERT INTo table1 VALUES(1,'text1')
CREATE TABLE table2
(id int,name text)
INSERT INTO table2 VALUES(1,'text1'),(2,'text2')
SELECT t1.id,t1.name,MATCH(t1.name) 
AGAINST 
  (
    (SELECT GROUP_CONCAT(name)
    FROM table2) IN NATURAL LANGUAGE MODE
  ) AS score
FROM table1 t1 
WHERE MATCH(t1.name) 
AGAINST 
  (
    (SELECT GROUP_CONCAT(name)
    FROM table2) IN NATURAL LANGUAGE MODE
  )
  
id | name  |                      score
-: | :---- | -------------------------:
 1 | text1 | 0.000000001885928302414186
SELECT t1.id,MATCH(t1.name) 
AGAINST 
  (
    (SELECT GROUP_CONCAT(name SEPARATOR ' ')
    FROM table2) IN NATURAL LANGUAGE MODE
  ) AS score
FROM table1 t1 
WHERE MATCH(t1.name) 
AGAINST 
  (
    (SELECT GROUP_CONCAT(name SEPARATOR ' ')
    FROM table2) IN NATURAL LANGUAGE MODE
  )
  
id | name  |                      score
-: | :---- | -------------------------:
 1 | text1 | 0.000000001885928302414186

dbfiddle here

我纠正了你的错误

SELECT 
    name,MATCH (name) AGAINST ((SELECT 
            name
        FROM
            (SELECT 
                name
            FROM
                active_ingredients UNION ALL SELECT 
                active_ingredients.name AS name
            FROM
                active_ingredients
            INNER JOIN temp_active_ingredients_aliases ON temp_active_ingredients_aliases.alias_name = active_ingredients.name) tbl
        GROUP BY name
        HAVING COUNT(*) = 1
        ORDER BY name) ) AS score
FROM
    (SELECT 
        alias_name AS name
    FROM
        temp_active_ingredients_aliases) db2
WHERE
    MATCH (name) AGAINST ((SELECT 
            name
        FROM
            (SELECT 
                name
            FROM
                active_ingredients UNION ALL SELECT 
                active_ingredients.name AS name
            FROM
                active_ingredients
            INNER JOIN temp_active_ingredients_aliases ON temp_active_ingredients_aliases.alias_name = active_ingredients.name) tb3
        GROUP BY name
        HAVING COUNT(*) = 1
        ORDER BY name) )

没有适当的数据,我无法测试它,只能删除语法错误

SELECT 
    name,MATCH (name) AGAINST ((    SELECT name
    FROM 
    (
        SELECT name 
            FROM active_ingredients

        UNION ALL
        
        SELECT active_ingredients.name as name
        FROM active_ingredients
        INNER JOIN temp_active_ingredients_aliases ON temp_active_ingredients_aliases.alias_name = active_ingredients.name

    ) tbl
    GROUP BY name
    HAVING count(*) = 1
    ORDER BY name) ) AS score
FROM
    (SELECT 
        alias_name AS name
    FROM
        temp_active_ingredients_aliases) db2
WHERE
    MATCH (name) AGAINST ((    SELECT name
    FROM 
    (
        SELECT name 
            FROM active_ingredients

        UNION ALL
        
        SELECT active_ingredients.name as name
        FROM active_ingredients
        INNER JOIN temp_active_ingredients_aliases ON temp_active_ingredients_aliases.alias_name = active_ingredients.name

    ) tbl
    GROUP BY name
    HAVING count(*) = 1
    ORDER BY name) )