MySQL使用条件更新多个行和列

时间:2015-07-03 01:35:20

标签: php mysql sqlite sql-update sql-insert

我正在制作一个程序,为4名玩家的任何视频游戏排队玩家

这是我的表结构:

CREATE TABLE IF NOT EXISTS `ozbah_users` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(255) NOT NULL,
  `user_status` varchar(2) NOT NULL,
  `user_priority` varchar(2) NOT NULL,
  `user_event_id` int(11) NOT NULL,
  `user_modifiedon` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`user_id`),
  KEY `user_event_id` (`user_event_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;


+-----------------+--------------+------+-----+-------------------+-------------
---+
| Field           | Type         | Null | Key | Default           | Extra
   |
+-----------------+--------------+------+-----+-------------------+-------------
---+
| user_id         | int(11)      | NO   | PRI | NULL              | auto_increme
nt |
| user_name       | varchar(255) | NO   |     | NULL              |
   |
| user_priority   | varchar(2)     | NO   |     | NULL              |
   |
| user_status     | varchar(2)   | NO   |     | NULL              |
   |
| user_event_id   | int(11)      | NO   | MUL | NULL              |
   |
| user_modifiedon | timestamp    | NO   |     | CURRENT_TIMESTAMP |
   |
+-----------------+--------------+------+-----+-------------------+-------------

这是插入的数据:

INSERT INTO `ozbah_users` (`user_id`, `user_name`, `user_priority`, `user_status`, `user_event_id`, `user_modifiedon`) VALUES
(1, 'A', 'A1', 'A', 1, '2015-06-26 19:22:45'),
(2, 'B', 'A2', 'A', 1, '2015-06-26 20:02:25'),
(3, 'C', 'A3', 'A', 1, '2015-06-26 20:22:16'),
(4, 'D', 'A4', 'A', 1, '2015-06-26 20:22:17'),
(5, 'E', 'W1', 'A', 1, '2015-06-26 20:22:19'),
(6, 'F', 'W2', 'A', 1, '2015-06-26 20:22:35'),
(7, 'G', 'W3', 'A', 1, '2015-06-26 20:22:55');
(8, 'H', 'W4', 'A', 1, '2015-06-26 20:32:15'),



mysql> SELECT * FROM `ozbah_users`
    -> ORDER BY `ozbah_users`.`user_modifiedon`  ASC;
+---------+-----------+---------------+-------------+---------------+-----------
----------+
| user_id | user_name | user_priority | user_status | user_event_id | user_modif
iedon     |
+---------+-----------+---------------+-------------+---------------+-----------
----------+
|       1 | A         |             A1 | A           |             1 | 2015-06-26
 22:22:45 |
|       2 | B         |             A2 | A           |             1 | 2015-06-26
 23:02:25 |
|       3 | C         |             A3 | A           |             1 | 2015-06-26
 23:22:16 |
|       4 | D         |             A4 | A           |             1 | 2015-06-26
 23:22:17 |
|       5 | E         |             W1 | A           |             1 | 2015-06-26
 23:22:19 |
|       6 | F         |             W2 | A           |             1 | 2015-06-26
 23:22:35 |
|       7 | G         |             W3 | A           |             1 | 2015-06-26
 23:22:55 |
|       6 | H         |             W4 | A           |             1 | 2015-06-26
 23:22:35 |
|       7 | I         |             W5 | A           |             1 | 2015-06-26

如您所见,user_priority为活跃或当前玩家An提供2个值Wn等待玩家 n 是玩家列表的队列数

活跃玩家An从1到4

等待玩家Wn从1到无限制

条件是活跃玩家是2支队伍

第1组:user_priority A1和A2

第2组:user_priority A3和A4

如果球队输掉比赛,两名球员都将移至等候名单的末尾, 并且等待名单中的第一名队员将移动到活动列表的末尾,并且获胜队将在活动玩家列表中移动到顶部

例如,团队2失败,因此A3和A4将移至等待列表的末尾,等待列表的前2个名称将结束活动列表

我只会更改玩家的名字和user_modifiedon

  

这里的主要问题是我想更新(Active的2行或4行)   球员)和(等待名单中的所有东西都将被转移)这就是什么   我需要!

因为在这种情况下我需要为每一行创建一个更新语句

+---------+-----------+---------------+-------------+---------------+-----------
----------+
| user_id | user_name | user_priority | user_status | user_event_id | user_modif
iedon     |
+---------+-----------+---------------+-------------+---------------+-----------
----------+
|       1 | A         |             A1 | A           |             1 | 2015-06-26
 22:22:45 |
|       2 | B         |             A2 | A           |             1 | 2015-06-26
 23:02:25 |
|       3 | E         |             A3 | A           |             1 | 2015-06-26
 23:22:16 |
|       4 | F         |             A4 | A           |             1 | 2015-06-26
 23:22:17 |
|       5 | G         |             W1 | A           |             1 | 2015-06-26
 23:22:19 |
|       6 | H         |             W2 | A           |             1 | 2015-06-26
 23:22:35 |
|       7 | I         |             W3 | A           |             1 | 2015-06-26
 23:22:55 |
|       8 | C         |             W4 | A           |             1 | 2015-06-26
 23:42:18 |
|       9 | D         |             W5 | A           |             1 | 2015-07-01
 22:32:49 |
+---------+-----------+---------------+-------------+---------------+-----------
----------+

我在java中使用2个arraylists创建类似的东西,比如这是java代码

这2个arraylist将包含 Active 等待玩家

的列表
 public class ArrayListDemo {

    public static void main(String[] args) {

        // create an array list
        ArrayList current = new ArrayList();
        ArrayList waiting  = new ArrayList();

        // Display size
        System.out.println("Initial size of current: " + current.size());
        System.out.println("Initial size of waiting: " + waiting.size());

        // add elements to the current array list
        current.add("A");
        current.add("B");
        current.add("C");
        current.add("D");
        System.out.println("Size of current after additions: " + current.size());

        // add elements to the waiting array list
        waiting.add("E");
        waiting.add("F");
        System.out.println("Size of waiting after additions: " + waiting.size());

        // display the array list
        System.out.println("Contents of current: " + current);
        System.out.println("Contents of waiting: " + waiting);

        // Remove elements from current array list

        current.add(waiting.get(0));
        current.add(waiting.get(1));

        System.out.println("Contents of current BEFORE Deleting: " + current);
        System.out.println("Size of current BEFORE Deleting: " + current.size());


        waiting.add(current.get(0));
        waiting.add(current.get(1));

        System.out.println("Contents of waiting BEFORE Deleting: " + waiting);
        System.out.println("Size of waiting BEFORE Deleting: " + waiting.size());


        current.remove(0);
        System.out.println("current.get(0): "+current.get(0));
        current.remove(0);

        System.out.println("current.get(0): "+current.get(0));

        System.out.println("Contents of current AFTER Deleting: " + current);

        waiting.remove(0);
        waiting.remove(0);

        System.out.println("Contents of waiting AFTER Deleting: " + waiting);



    }

}

这是输出

Initial size of current: 0
Initial size of waiting: 0
Size of current after additions: 4
Size of waiting after additions: 2
Contents of current: [A, B, C, D]
Contents of waiting: [E, F]
Contents of current BEFORE Deleting: [A, B, C, D, E, F]
Size of current BEFORE Deleting: 6
Contents of waiting BEFORE Deleting: [E, F, A, B]
Size of waiting BEFORE Deleting: 4
current.get(0): B
current.get(0): C
Contents of current AFTER Deleting: [C, D, E, F]
Contents of waiting AFTER Deleting: [A, B]

我的问题是,更新多行并保持序列正确的最佳SQL查询是什么?

  

这里的主要问题是我想更新(Active的2行或4行)   球员)和(等待名单中的所有东西都将被转移)这就是什么   我需要!

谢谢

1 个答案:

答案 0 :(得分:1)

如果您要删除W2并需要将所有较高级别的内容删除,则可以执行以下操作:

SET @delete = 2;

UPDATE ozbah_users
SET user_priority = 
    CASE
        WHEN user_priority LIKE 'W%' AND user_priority < CONCAT('W', @delete) 
            THEN CONCAT('A', SUBSTR(user_priority, 2) + 2
        WHEN user_priority = CONCAT('W', @delete) 
            THEN -1
        WHEN user_priority LIKE 'W%' AND user_priority > CONCAT('W', @delete) 
            THEN CONCAT('W', SUBSTR(user_priority, 2) - 2)
    END
WHERE user_priority LIKE 'W%'