2015-11-10 209 views
0

我有一個表,我試圖在name排序順序

select * from table where name like '%apple%m%' 
+-------+-------------------------------+------------------------+ 
| id |    name    |   cat   | 
+-------+-------------------------------+------------------------+ 
| 2757 | Apple MacBook     | Laptops & Accessories | 
| 2777 | Apple Bottom Tops    | Western Wear   | 
| 2752 | Apple Monitors    | Desktop Components  | 
| 2756 | Apple Desktop & Monitors  | Desktops & Accessories | 
| 2778 | Apple Bottom Tunics   | Tops     | 
| 2776 | Apple Selector & Smart Box | Video & TV Accessories | 
| 2787 | Apple Pie Pyjamas    | Girl Clothes   | 
| 2773 | Apple Selector & Smart Box | TV & Video Accessories | 
| 2780 | Apple Fun Card Games   | Toys     | 
| 38304 | Snapple Mixer juicer grinders | Kitchen Appliances  | 
+-------+-------------------------------+------------------------+ 

我想這與開始顯示值進行排序「過濾器「蘋果M」值蘋果」和‘m’象下面這樣:

+------+--------------------------+------------------------+ 
| id |   name   |   cat   | 
+------+--------------------------+------------------------+ 
| 2757 | Apple MacBook   | Laptops & Accessories | 
| 2752 | Apple Monitors   | Desktop Components  | 
| 2756 | Apple Desktop & Monitors | Desktops & Accessories | 
|  | ---Rest all after this-- |      | 
+------+--------------------------+------------------------+ 
+0

爲什麼在Apple Monitors之後有'Apple Desktop'? 'D'在'M'之前。 – Barmar

回答

1

用途:

name LIKE 'Apple%m%'1時的名字,當沒有模式,0匹配,所以這會先排列匹配的名字。然後它將按照每個組中的名稱排序。

0

我認爲你可以使用一些簡單的像下面這樣:

SELECT * FROM your_table WHERE name LIKE 'apple% m%' ORDER BY name; 

上述查詢首先將獲得與名稱由'apple '開始行,然後將查詢結果排序。

如果您需要區分大小寫,那麼你可以使用:

SELECT * FROM your_table WHERE name LIKE 'Apple% m%' COLLATE utf8_bin ORDER BY name; 

Case sensitivity in like

+0

嗨科斯塔斯,如果我做的命令由上名字它會列出「蘋果底部上衣」和「蘋果底部長袍」第一 – Manoj

+0

哦,在你預期的結果,你有蘋果臺式機和顯示器,從而爲這個原因,我還以爲你想要那個。 –

+0

你的意思是你只想要一個單詞是'Apple'而另一個單詞是以'm'開頭的結果? –