English 中文(简体)
更正
原标题:determine order in a ORDER BY statement
  • 时间:2012-04-12 11:28:13
  •  标签:
  • php
  • mysql

我有以下声明:

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

In this case the result will be alphabetically order based on the field scene .

有可能根据以下字母顺序而不是按字母顺序排列结果:

$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
问题回答

最为清洁的解决办法是:,并具体规定Scene栏应当使用。 如果你这样做,询问将简单地改为<代码>。 页: 1 FIELD(Scene) 没有意义——你是否检查了.FIELD?

除此以外,你还可以根据<编码>战略_REPLACE,将data改为校对,而不是在校对,但我却胜过。

当然,你可以回避所有这一切,并在项目组合中进行分类,因为项目组合将更加方便,但这种办法将无法解决与必须自行分类的分局合并的提问。

回答是最佳解决办法。 但是,在这里,对你们来说,是一种残酷的“基地”解决办法。

$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"; 

There are two solutions: 1) order after query directly in code. the drawback is you always need to gather all the results so you could have performance problems. 2) add a column to the table with the ordering values based on the index of your array.

也许你可以创造我的奇克勒功能,以命令......,但我不知道习惯的神话功能。

......和约恩提出的地雷前2个雷区的解决办法:D

尽管若能回答是正确的办法,但你可能希望通过建立一个表格(ID, C)来尝试迅速和 d脏的版本,其中国际发展法(autoincrement)是分类,而C是VARCHAR(1)。

With joins the ordering could be achieved to whatever level is required.

$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);

注:虽然可以快速地进行这项工作,但搁置问题本身将比以前缓慢。

如果你想用粗略的方式来替代一个领域,那么你需要一种在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;

At this point $sql will contain the following query:

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`




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签