2014-01-28 51 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 ;