-2

私のテーブル:

    6   2/3/2013 row1
10      2/2/2013 row2
    1   1/3/2013 row3
2       1/3/2013 row4

column1 に値がある場合 (column2 は空になります) column3 で行を並べ替えます。column2 に値を持つ行ではなく、column1 に値を持つ行のみをソートします。

column2 に値がある場合 (column1 は空になります)、 column2 に従って並べ替えます。column1 に値を持つ行ではなく、column2 に値を持つ行のみをソートします。

並べ替え:

最初の反復:

First column  column1 row1 is empty  and column2 has value so comparing row1 and row3, 1 is minimum so is in top.

    1   1/2/2013 row1
10      2/2/2013 row2
    6   2/3/2013 row3
2       1/3/2013 row4

2 回目の繰り返し:

First column column1  row2 is there  so it comparing row2 and row4 for column3 date 1/3/2013 < 2/2/2013 so swap .

    1   1/2/2013 row1
2       1/3/2013 row2
    6   2/3/2013 row3
10      2/2/2013 row4

3 回目の反復:

First column column1  row3 is  empty in first column so and  comparing column2  since 1 < 6 so no changes.

    1   1/2/2013 row1
2       1/3/2013 row2
    6   2/3/2013 row3
10      2/2/2013 row4

4 回目の反復:

First column column1  row4 is there  so it comparing row2 and row4 for column3 date 1/3/2013 < 2/2/2013 so nothing.

    1   1/2/2013 row1
2       1/3/2013 row2
    6   2/3/2013 row3
10      2/2/2013 row4


Final Result After Sorting:

    1   1/2/2013 row1
2       1/3/2013 row2
    6   2/3/2013 row3
10      2/2/2013 row4

PHP/mysqlでそれを行う良い方法はありますか?

私は解決策を見つけることができないPHPソート機能を試しました。

ありがとうございました。

4

1 に答える 1

1

面白い問題ありがとうございます。解決策は次のようです。

SELECT *,col1+col2 p FROM test ORDER BY p;

これは、このフィドルで提供したテスト データに対して機能します: http://www.sqlfiddle.com/#!2/3c7e2/4

この答えは、あなたが言うように、値を持つ列が 1 つだけの場合にのみ正しいでしょう。

于 2013-06-24T07:52:07.803 に答える