2012-04-12 39 views
2

我有一個select語句:確定在ORDER ORDER BY語句

$search = "SELECT Scene, Divinite, Attestation 
    FROM SceneDivList 
    WHERE BINARY Divinite = '$target' 
    ORDER BY FIELD(Scene); 
$result = mysql_query($search, $con); 

在這種情況下,結果將是字母順序根據現場「場景」。

是否有可能爲了不按字母順序,但基於以下的「字母表」的結果:

$alphabet = array(1 => '-' , 2 => ',' , 3 => '.' , 4 => "A", 
        5 => "j", 6 => "a", 7 => "w", 8 => "b", 
        9 => "p", 10 => "f", 11 => "m", 12 => "n", 
        13 => "r", 14 => "h", 15 => "H", 16 => "x", 
        17 => "X", 18 => "s", 19 => "S", 20 => "q", 
        21 => "k", 22 => "g", 23 => "t", 24 => "T", 
        25 => "d", 26 => "D"); 

結果應該再是:

SCENE   ATTESTATION 
jnD-Hr-m-nms,t DI.80,11 
jrp    DI.26,12 
jrT,t   DI.116,17 
aAb,t   DI.138,8 
anx    DI.12,5 
antjw   DI.148,10 
wADw-msdm,t  DI.144,11 
bHsw   DI.115,9 
pr-n-nb=f   DI.17,7 

回答

2

最乾淨的解決辦法是define your own custom collation和指定Scene列應該使用它。如果你這樣做,查詢將簡單地讀取ORDER BY Scene,就是這樣(順便說一句,ORDER BY FIELD(Scene)沒有意義 - 你看看FIELD做什麼?)。

除此之外,你可以砍一起基於STR_REPLACE到給整理,而不是反其道而行之適應數據的噩夢,但我不會去那裏。

當然,您可以避開所有這些,並在PHP中進行排序,這樣會更加方便,但這種方法不適用於加入子查詢的查詢,這些查詢必須自行排序。

0

有兩種解決方案: 1)直接在代碼中查詢後的順序。缺點是你總是需要收集所有的結果,所以你可能會遇到性能問題。 2)使用基於數組索引的排序值向表中添加一列。

,也許你可以創建一個MySQL的函數命令上...但我不知道足夠的定製MySQL的函數

...和喬恩的解決方案提出了2分鐘,我之前:d

0

雖然Jon的回答是正確的方法,但您可能需要通過設置ID(自動增量)整數且C爲VARCHAR(1)的表(ID,C)嘗試快速和骯髒的版本。

隨着連接順序可以達到任何級別的要求。

$search = "SELECT s.Scene, s.Divinite, s.Attestation 
FROM SceneDivList s 
INNER JOIN CustomOrder o1 ON o1.C = SUBSTRING(s.Scene,0,1) 
INNER JOIN CustomOrder o2 ON o2.C = SUBSTRING(s.Scene,1,1) 
INNER JOIN CustomOrder o3 ON o3.C = SUBSTRING(s.Scene,2,1) 
WHERE BINARY s.Divinite = '$target' 
ORDER BY o1.ID, o2.ID, o3.ID, SUBSTRING(s.Scene,4); 
$result = mysql_query($search, $con); 

注:雖然這是可以做到快速&髒查詢本身會比以前慢的方式。

1

喬恩的答案是最好的解決方案。但這裏有你

$alphabet = array(1 => '-' , 2 => ',' , 3 => '.' , 4 => "A", 
       5 => "j", 6 => "a", 7 => "w", 8 => "b", 
       9 => "p", 10 => "f", 11 => "m", 12 => "n", 
       13 => "r", 14 => "h", 15 => "H", 16 => "x", 
       17 => "X", 18 => "s", 19 => "S", 20 => "q", 
       21 => "k", 22 => "g", 23 => "t", 24 => "T", 
       25 => "d", 26 => "D"); 

$replace_engine = 'Scene';     
foreach($alphabet as $k=>$char) { 
    $charint = sprintf("%03s", $k); 
    $replace_engine = "REPLACE(".$replace_engine.",'$char','$charint')"; 
} 


$query = "SELECT $replace_engine as myalphabet, Scene FROM SceneDivList ORDER BY myalphabet"; 
+0

需要是遞歸的。 – pp19dd 2012-04-12 12:16:19

+0

這是遞歸的 – 2012-04-12 12:21:21

+0

我的立場部分糾正。沒有抓住夾住字符串的竅門,儘管沒有通過標準的遞歸。正在+1。 – pp19dd 2012-04-12 12:28:58

0

殘酷的PHP解決方案,如果你想窮舉字符串替換一個字段,排序它,你需要在PHP中的遞歸函數生成的字符串:

function replace($fields) { 
    if(empty($fields)) return("`Scene`"); 

    $t = each($fields); 
    $pop = array_shift($fields); 

    return(sprintf(
     "replace(%s, '%s', '%s')", 
     replace($fields), 
     $t['key'], 
     str_pad($t['value'], 3, '0', STR_PAD_LEFT) 
    )); 
} 

$alphabet = array(1 => '-' , 2 => ',' , 3 => '.' , 4 => "A", 
        5 => "j", 6 => "a", 7 => "w", 8 => "b", 
        9 => "p", 10 => "f", 11 => "m", 12 => "n", 
        13 => "r", 14 => "h", 15 => "H", 16 => "x", 
        17 => "X", 18 => "s", 19 => "S", 20 => "q", 
        21 => "k", 22 => "g", 23 => "t", 24 => "T", 
        25 => "d", 26 => "D"); 



$sql = sprintf(
"SELECT Scene, Divinite, Attestation, %s as `neworder` 
FROM SceneDivList 
WHERE BINARY Divinite = '%s' 
ORDER BY `neworder`", 
    replace(array_flip($alphabet )), 
    "target <- whatever that is" 
); 

echo $sql; 

此時$ SQL將包含以下查詢:

SELECT Scene, Divinite, Attestation, 
replace(replace(replace(replace(replace(
replace(replace(replace(replace(replace(
replace(replace(replace(replace(replace(
replace(replace(replace(replace(replace(
replace(replace(replace(replace(replace(replace(
`Scene`, 'D', '026'), 'd', '025'), 'T', '024' 
), 't', '023'), 'g', '022'), 'k', '021'), 'q', '020' 
), 'S', '019'), 's', '018'), 'X', '017'), 'x', '016' 
), 'H', '015'), 'h', '014'), 'r', '013'), 'n', '012' 
), 'm', '011'), 'f', '010'), 'p', '009'), 'b', '008' 
), 'w', '007'), 'a', '006'), 'j', '005'), 'A', '004' 
), '.', '003'), ',', '002'), '-', '001' 
) as `neworder` 
FROM SceneDivList 
WHERE BINARY Divinite = 'target <- whatever that is' 
ORDER BY `neworder`