2016-01-11 78 views
5
$qry="select * from table where category='car' or title='car' or description='car'"; 

但我希望輸出按類別先列出行,然後標題和描述。mysql:按列名排列結果

****編輯:其實我使用的是像運營搜索**

例子:

id category title description 
    1 car 
    2 car 
    3   car 
    4   car 
    5   car 
    6     car 

有比使用其他工會的任何方式?
在此先感謝。

+1

您是否在尋找ORDER BY?對不起,但不清楚.. – Kamal

+0

我覺得秩序是行不通的。如果你認爲這將有助於回答我的問題pllease發帖 –

+0

你可以按照戈登的答案......它比我們更好。 – devpro

回答

9

您可以使用ORDER BY使用正確的鍵來完成此操作。在MySQL中,你可以這樣做:

ORDER BY (category = 'car') DESC, 
     (title = 'car') DESC, 
     (description = 'car') DESC 

的MySQL把布爾表達式爲整數的數值範圍內,其中0爲假,1爲真。所以DESC首先將真正的版本。

您還可以簡化WHERE條款,如果你喜歡:

WHERE 'car' IN (category, title, description) 
+0

我很抱歉我的朋友,但是h贏得了這場比賽...贏得我的投票歡呼隊友... – devpro

0

您可以在查詢中有多個ORDER BY條款。

select *from table where category='car' or title='car' or description='car' ORDER BY category DESC, title DESC, description DESC 

查看此答案以供參考。 mysql query order by multiple items

+0

應該按降序排列。 –

+0

'*' – RiggsFolly

+0

之後還需要一個空格也許不是,但肯定讀得更好一個 – RiggsFolly

1

可以使用ORDER BY多個列,如:

SELECT * FROM tablename ORDER BY category DESC, title DESC, description DESC 

我已經嘗試過了,它worked

enter image description here

+0

你忘了我想'WHERE'caluse。 –

+0

謝謝兄弟的幫助 –

2

您可以使用這一說法得到這樣的結果:

SELECT * FROM mytable 
WHERE (category='car' OR title='car' OR description='car') 
ORDER BY category = 'car' DESC, 
title = 'car' DESC, 
description = 'car' DESC 

它是如何工作的?

它將按照查詢中提到的DESCsequentially來設置數據的順序。您可以根據需要更改序列。

+0

謝謝兄弟的幫助 –

1
Try this , 

SELECT * FROM table where 'car' IN (category, title, description) ORDER BY category DESC, title DESC, description DESC 
0

嘗試此查詢:

select * from table 
where category='car' or title='car' or description='car' 
order by 1 desc, 2 desc, 3 desc