sql >> データベース >  >> RDS >> Mysql

特定のサイズを作成する

    あなたの質問はよくわかりませんが、リストを並べ替える例を次に示します...

    DROP TABLE IF EXISTS my_table;
    
    CREATE TABLE my_table
    (image CHAR(1) NOT NULL PRIMARY KEY 
    ,position INT NOT NULL
    );
    
    INSERT INTO my_table VALUES
    ('A',1),
    ('B',2),
    ('C',3),
    ('D',4),
    ('E',5),
    ('F',6);
    

    したがって、位置5の画像を位置2にドラッグするとします...

    SET @old_position = 5;
    SET @new_position = 2;
    
    SELECT image
         , position old_order
         , ROUND(CASE WHEN position NOT BETWEEN LEAST(@old_position,@new_position) AND GREATEST(@old_position,@new_position) 
                      THEN position 
                      WHEN position = @old_position THEN @new_position
                      ELSE position+(((@new_position<@old_position)-.5)*2)
                      END 
                ,0) new_order 
      FROM my_table;
    
    +-------+-----------+-----------+
    | image | old_order | new_order |
    +-------+-----------+-----------+
    | A     |         1 |         1 |
    | B     |         2 |         3 |
    | C     |         3 |         4 |
    | D     |         4 |         5 |
    | E     |         5 |         2 |
    | F     |         6 |         6 |
    +-------+-----------+-----------+
    

    これはより完全な例で、PHPを使用してHTMLに出力しています...おそらく他の誰かがそれをきれいにすることができます...

    <?php
    //simple_sorter.php
    //Preamble
    
    /*
    A simple row repositioning script.
    This is using a simple $_GET to determine which row is repositioned.
    So you need to supply a source and a target in the url, e.g.:
    
    https://path/to/simple_sorter.php?old_position=5&new_position=2
    
    There is no error checking, so it can quite easily fall apart, and because   
    the SELECT comes befpre the UPDATE, you won't see any changes until the 
    next time you load the page.
    */
    
    //Data Creation Statements
    
    /*
    DROP TABLE IF EXISTS my_table;
    
    CREATE TABLE my_table
    (image CHAR(1) NOT NULL PRIMARY KEY
    ,position INT NOT NULL
    );
    
    INSERT INTO my_table VALUES
    ('A',1),
    ('B',2),
    ('C',3),
    ('D',4),
    ('E',5),
    ('F',6);
    */
    
    require('path/to/pdo/connection/stateme.nts');
    
    //My understanding is that the following is needed 
      in order to replace (every instance within the 
      query of) :old_position and :new_position with 
      their corresponding values
    
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, TRUE);
    
    //and now to the code...
    
    $query = "
    SELECT *
      FROM my_table
     ORDER
        BY position
    ";
    $stmt  = $pdo->prepare($query);
    $stmt->execute();
    $data  = $stmt->fetchAll();
    print_r($data);
    
    $query = "
    UPDATE my_table x
      JOIN
         ( SELECT image
                , position old_order
                , ROUND(CASE WHEN position NOT BETWEEN LEAST(:old_position,:new_position) AND GREATEST(:old_position,:new_position)
                             THEN position
                             WHEN position = :old_position THEN :new_position
                             ELSE position+(((:old_position>:new_position)-.5)*2)
                             END
                       ,0) new_order
             FROM my_table
         ) y
        ON y.image = x.image
       SET position = new_order
    ";
    
    $old_position  = $_GET['old_position'];
    $new_position  = $_GET['new_position'];
    
    $stmt  = $pdo->prepare($query);
    $stmt->execute(array('old_position' => $old_position,'new_position' => $new_position));
    
    ?>
    

    出力(たとえば、使用された値と頻度によって異なります)...

    Array
    (
        [0] => Array
            (
                [image] => A
                [position] => 1
            )
    
        [1] => Array
            (
                [image] => D
                [position] => 2
            )
    
        [2] => Array
            (
                [image] => E
                [position] => 3
            )
    
        [3] => Array
            (
                [image] => B
                [position] => 4
            )
    
        [4] => Array
            (
                [image] => C
                [position] => 5
            )
    
        [5] => Array
            (
                [image] => F
                [position] => 6
            )
    
    )
    



    1. postgresql日時に日数を追加する方法

    2. Microsoft SQL Serverによるアクセス–SSISを使用した大規模なデータセットのインポート

    3. 文字列をSQL時間にキャストします

    4. MySQLのTrue/False vs 0/1