6

の理解ORDER BYに問題がありMySQLます。3つの基準でテーブルをソートする必要があります

1 - 最初にTYPE_OF_WORKで並べ替えたいので、すべてのデータはアルファベット順でなければなりません。

dech_rap_bus
dech_rap_bus
ger_dem_dech_bus
ger_dem_dech_bus
ger_dem_stp_pp
...

結果 => http://sqlfiddle.com/#!2/b2a858/6

2 - 2 番目にPROJECT_VERSIONで並べ替えたいので、すべてのデータはアルファベット順である必要がありますが、次のように 1 の基準を尊重します。

dech_rap_bus            V123-V1234
dech_rap_bus            V300
ger_dem_dech_bus        V123-V1234  
ger_dem_dech_bus        V300
ger_dem_stp_pp          V123-V1234  

結果 => http://sqlfiddle.com/#!2/b2a858/7

したがって、 12は完全に機能しています。

3 - この後、列not_existingでソートしたい

結果 => http://sqlfiddle.com/#!2/b2a858/5

そして、それが実際に何をするのかはわかりませんが、結果が表示されません...

dech_rap_bus V300

ここで、NOT_EXISTING列が1の場合は最後になり、NOT_EXISTING = 1の場合はテーブルの最後以外のすべてを並べ替えます。

2 選択の UNION が役立つだろうと自分に言い聞かせました

/* Selecting all data where  not_existing is not 1 or NULL ---> working good! */
(
    SELECT 
    * 
    FROM 
    atm_mti_view 
    WHERE 
    project_function='FRS01' AND 
    on_big_project_id = 12 AND
    (not_existing != 1 OR not_existing IS NULL)
    ORDER BY
    type_of_work ASC,
    project_version ASC
)

UNION

/* Selecting all data where  not_existing is 1 ---> working good! */
(
    SELECT 
    * 
    FROM 
    atm_mti_view 
    WHERE 
    project_function='FRS01' AND 
    on_big_project_id = 12 AND
    not_existing = 1
    ORDER BY
    type_of_work ASC,
    project_version ASC
)

しかし、このコードが行うことは、存在しない dech_rap_bus を最後に配置することです。良いのですが、バージョンの並べ替えが台無しになります。なぜですか???

ここで結果を参照してください=> http://sqlfiddle.com/#!2/b2a858/8

何故ですか?2 つの選択結果をMERGEしたいだけですが、何が間違っていますか?

4

3 に答える 3

2

これはあなたが望むものをあなたに与えませんか?

http://sqlfiddle.com/#!2/b2a858/26

最初に not_existing でソートするだけですか?

ここで何が問題なのですか?並べ替えがありますが、最後に not_existing レコードがあります-これらも同じ方法で並べ替えられます

于 2012-08-07T12:20:09.683 に答える
1

UNIONこのクエリでこれを達成できる必要はありません。

SELECT *
FROM atm_mti_view
WHERE project_function='FRS01' AND
      on_big_project_id = 12          
ORDER BY IF(not_existing = 1, 1, 0) ASC,
         type_of_work ASC,
          project_version ASC;
于 2012-08-07T12:22:35.687 に答える
1

もしあなたがそうするなら

order by 
 (case when not_existing is null then 0 else not_existing end) desc
,type_of_work ASC,
project_version ASC

それは最初に来ます。

dech_rap_bus のプロジェクト値が異なるため、クエリは順序付けされていません

TYPE_OF_WORK    PROJECT_VERSION 
dech_rap_bus    V123-V1234  
dech_rap_bus    V300
于 2012-08-07T12:23:00.803 に答える