如何在连接多个表的MySQL中执行全文搜索

时间:2019-01-03 11:08:24

标签: mysql search full-text-search e-commerce

我正在使用MySQL创建一个电子商务网站。我已成功创建数据并将数据插入数据库。

这是我的数据库架构

table: categories                    table: product_types
+----+--------------+            +----+-------------+------------+
| id | name         |            | id | category_id | name       |
+----+--------------+            +----+-------------+------------+
|  1 | Electronics  |            |  1 |           1 | Smartphone |
|  2 | Fashion      |            |  2 |           1 | Speakers   |
+----+--------------+            +----+-------------+------------+

    table: products
+----+-----------------+-------------+-------------------+-------+
| id | product_type_id | category_id | name              | price |
+----+-----------------+-------------+-------------------+-------+
|  1 |               1 |           1 | Samsung Galaxy A3 |   300 |
|  2 |               1 |           1 | Samsung Galaxy A7 |   400 |
+----+-----------------+-------------+-------------------+-------+

    table: options                         table: option_values
+----+-----------------+-------+       +----+-----------+------------+
| id | product_type_id | name  |       | id | option_id | name       |
+----+-----------------+-------+       +----+-----------+------------+
|  1 |               1 | RAM   |       |  1 |         1 | 512 MB     |
|  2 |               1 | Screen|       |  2 |         1 | 1 GB       |
|  3 |               1 | OS    |       |  3 |         3 | Android 5  |
+----+-----------------+-------+       |  4 |         3 | Android 6  |
                                       |  5 |         2 | HD         |
                                       |  6 |         2 | FHD        |
                                       +----+-----------+------------+
     table: product_option_values
+----+------------+-----------+-----------------+
| id | product_id | option_id | option_value_id |
+----+------------+-----------+-----------------+
| 15 |          1 |         1 |               1 |
| 16 |          1 |         2 |               5 |
| 17 |          1 |         3 |               3 |
| 18 |          2 |         1 |               2 |
| 19 |          2 |         2 |               6 |
| 20 |          2 |         3 |               4 |
+----+------------+-----------+-----------------+

搜索必须通过每个表的name列触发,并从name表返回priceproducts。 问题是我不知道如何执行将所有表连接在一起的全文本搜索。

有什么简单的方法吗?

1 个答案:

答案 0 :(得分:1)

您需要一个查询,其中每个表上有LEFT JOIN个查询,以基于全文搜索功能MATCH的条件进行搜索,并带有WHERE子句以筛选出不匹配的记录。 SELECT DISTINCT确保您不会看到重复的内容。

我们需要将每个表中的JOIN标准手动调整为productsoption_values是最复杂的情​​况,因为它不直接引用products(附加联接需要product_option_values上的名称,下面是别名pov

SELECT DISTINCT p.name, p.price
FROM 
    products p
    LEFT JOIN categories c
        ON  MATCH(c.name) AGAINST('foo' IN NATURAL LANGUAGE MODE)
        AND c.id = p.category_id
    LEFT JOIN product_types pt
        ON  MATCH(pt.name) AGAINST('foo' IN NATURAL LANGUAGE MODE)
        AND pt.category_id = p.category_id
    LEFT JOIN options o
        ON  MATCH(o.name) AGAINST('foo' IN NATURAL LANGUAGE MODE)
        AND o.product_type_id = p.product_type_id  
    LEFT JOIN product_option_values pov 
        ON  pov.product_id = p.id 
    LEFT JOIN option_values ov
        ON  MATCH(ov.name) AGAINST('foo' IN NATURAL LANGUAGE MODE)
        AND ov.id = pov.option_value_id 
WHERE 
    COALESCE(c.id, pt.id, o.id, ov.id) IS NOT NULL