MySQL FULLTEXT匹配不同的结果

时间:2016-10-13 00:34:31

标签: mysql database

我想要运行FULLTEXT搜索并仅返回具有不同(唯一)product_name的结果,但DISTINCT(product_name)似乎不适用。任何有关正确方向的帮助或指示都将受到极大的欢迎。

MySQL QUERY:

SELECT DISTINCT(product_name), sku
FROM products
WHERE MATCH(product_name)
AGAINST('Flixonase' IN NATURAL LANGUAGE MODE)
LIMIT 5

当前结果:

product_name                     sku
FLIXONASE NASAL 24 HR SPRAY 60D  9300673808851
FLIXONASE NASAL 24 HR SPRAY 120D 9400600822438
FLIXONASE NASAL 24 HR SPRAY 60D  930067308851
FLIXONASE NASAL 24 HR SPRAY 60D  9300673831972
FLIXONASE NASAL 24 HR SPRAY 120D 9300673832146

注意: 第1,3和3行4有重复的product_names。这就是我想要消除的。

结果我想要

product_name                      sku
FLIXONASE NASAL 24 HR SPRAY 60D   9300673808851
FLIXONASE NASAL 24 HR SPRAY 80D   9400600822438
FLIXONASE NASAL 24 HR SPRAY 100D  930067308851
FLIXONASE NASAL 24 HR SPRAY 120D  9300673831972
FLIXONASE NASAL 24 HR SPRAY 140D  9300673832146

2 个答案:

答案 0 :(得分:0)

DISTINCT不适用于单个列,它指的是整行。如果要为特定列获取单行,请使用GROUP BY

SELECT product_name, sku
FROM products
WHERE MATCH(product_name)
    AGAINST('Flixonase 24hr Allergy Relief Nasal Spray 60 Doses' IN NATURAL LANGUAGE MODE)
GROUP BY product_name
ORDER BY MATCH(product_name)
    AGAINST('Flixonase 24hr Allergy Relief Nasal Spray 60 Doses' IN NATURAL LANGUAGE MODE) DESC
LIMIT 5

答案 1 :(得分:0)

这里的问题相同。现在用GROUP BY解决。
我的代码:
SELECT * FROM products WHERE MATCH(description) AGAINST('my product description' IN NATURAL LANGUAGE MODE) GROUP BY product_name LIMIT 10

感谢@Barmar