[英]MySQL update multiple rows and columns with condition
我正在开发一个程序,将任何 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
有 2 个值An
表示活跃或当前玩家Wn
表示等待玩家n是玩家列表的队列数
活跃玩家An
从 1 到 4
等待玩家Wn
从 1 到无限制
条件是现役球员是两支球队
团队 1: user_priority
A1 和 A2
团队 2: user_priority
A3 和 A4
如果球队输了比赛,两名球员都将移至等待名单的末尾,等待名单中的前两名球员将移至活跃名单的末尾,获胜的球队将移至活跃球员名单的顶部
例如团队2输这么A3和A4将移动到等待名单和候选人名单的第2名最终会结束活动列表
我只会更改玩家和user_modifiedon
的名称
这里的主要问题是我想更新(来自活跃玩家的 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 |
+---------+-----------+---------------+-------------+---------------+-----------
----------+
我使用 2 个数组列表在 java 中创建了类似的东西,就像这是 java 代码
这 2 个数组列表将包含活动和等待玩家的列表
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 查询是什么?
这里的主要问题是我想更新(来自活跃玩家的 2 或 4 行)并且(等待列表中的所有内容都将被转移)这就是我需要的!
谢谢你
如果您要删除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%'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.