2011-05-26 50 views
1

我有一個MySQL select語句並希望將其作爲一個查詢。MySQL中的SQL排序

select * from users where... 

where子句將有四個部分,他們需要訂購這種方式:

1. where description = "query" 

2. where description = "%query%" 

3. where description = "q1" or description = "q2" 

4. where description = "%q1%" or description = "%q2%" 
+0

這個問題類似:http://stackoverflow.com/questions/396748/ordering-by-the-order-of-values-in-a-sql-in-clause使用域,在給定的鏈接 – 2011-05-26 06:27:15

+0

該字段如何工作? – cdub 2011-05-26 06:38:10

+0

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_field – 2011-05-26 06:39:34

回答

2

一個解決方案的結果將UNION的結果添加到每個查詢的虛假OrderBy字段中。

SELECT 1 as OrderBy, * 
FROM  users 
WHERE description = "query" 
UNION ALL 
SELECT 2 as OrderBy, * 
FROM  users 
WHERE description = "%query%" 
... 
ORDER BY 
     OrderBy 
+0

好吧,這是更多我想要的,我現在會測試它,但它看起來不錯! – cdub 2011-05-26 06:52:34

+0

正是我想要的! – cdub 2011-05-26 06:58:24

+0

@chris - 你不客氣。 – 2011-05-26 07:05:25

0

我認爲下面會產生你需要

select * from users 
where description like "%query%" 
    or description like "%q1%" 
    or description like "%q2%" 
+0

是的,但是我希望查詢部分在q1和q2部分之前全部排序 – cdub 2011-05-26 06:48:48