我的列如下
name | cat
----------------------------------------
ala | games
alaa | software
aha | games
asd | games
aad | software
aas | software
asd | books
aad | software
aas | books
所以我有三个查询,如下面
SELECT * FROM `table` WHERE MATCH(`name`) AGAINST ('a*' IN BOOLEAN MODE) AND cat='games' LIMIT 2
SELECT * FROM `table` WHERE MATCH(`name`) AGAINST ('a*' IN BOOLEAN MODE) AND cat='software' LIMIT 2
SELECT * FROM `table` WHERE MATCH(`name`) AGAINST ('a*' IN BOOLEAN MODE) AND cat='books' LIMIT 2
所以我想先获得2 result from each category
(游戏,书籍,软件)......现在我使用这三个查询..如何将它们组合成一个查询..
P.s这只是一个例子..我正在处理多达10个类别..我想要一些不浪费太多资源的东西
答案 0 :(得分:1)
假设这些查询有效,请使用UNION
:
SELECT DISTINCT * FROM `table` WHERE MATCH(`name`)
AGAINST ('a*' IN BOOLEAN MODE) LIMIT 2 WHERE cat='games'
UNION
SELECT DISTINCT * FROM `table` WHERE MATCH(`name`)
AGAINST ('a*' IN BOOLEAN MODE) LIMIT 2 WHERE cat='books'
UNION
SELECT DISTINCT * FROM `table` WHERE MATCH(`name`)
AGAINST ('a*' IN BOOLEAN MODE) LIMIT 2 WHERE cat='software'
关于优化,documentation状态"当将LIMIT row_count与DISTINCT组合时,MySQL会在找到row_count唯一行时立即停止。"所以你应该添加它来限制各个查询的资源。