2014-01-28 53 views
0

下面是我对搜索关键字查询,然后ORDER BY FIELdmysql命令

SELECT * FROM mall WHERE mall_status = '1' AND 
(mall_name LIKE '%Cap%' OR mall_name LIKE '%Square%' OR mall_name = 'Cap Square' OR 
tag LIKE 'Cap Square,%' OR tag LIKE '%,Cap Square' OR tag LIKE '%, Cap Square' OR tag LIKE '%,Cap Square,%' OR tag LIKE '%, Cap Square,%' OR tag = 'Cap Square') 
ORDER BY FIELD(state_id, 14, 10, 5, 4, 1, 6, 11, 3, 2, 7, 8, 15, 12, 13) ASC , mall_name LIMIT 0,30 

,结果将显示基于state_id秩序,但现在怎么返回第一行最佳匹配只有,然后按state_id(ORDER BY FIELD)。最佳匹配类似mall_name = 'Cap Square'

所以结果是这样的:

Cap Square (state id: 10) 
Central Square (state id: 14) 
Berjaya Times Square (state id: 14) 
Oasis Square (state id: 10) 
Shaftsbury (state id: 5) 
Penang Times Square (state id: 4) 

回答

1

我想你可以做这样的排序:

SELECT * 
FROM mall 
WHERE mall_status = '1' AND 
     (mall_name LIKE '%Cap%' OR mall_name LIKE '%Square%' OR mall_name = 'Cap Square' OR 
     tag LIKE 'Cap Square,%' OR tag LIKE '%,Cap Square' OR tag LIKE '%, Cap Square' OR 
     tag LIKE '%,Cap Square,%' OR tag LIKE '%, Cap Square,%' OR tag = 'Cap Square' 
    ) 
ORDER BY mall_name = 'Cap Square' desc, 
     FIELD(state_id, 14, 10, 5, 4, 1, 6, 11, 3, 2, 7, 8, 15, 12, 13) ASC , mall_name 
LIMIT 0, 30 ;