简体   繁体   中英

MySQL update multiple rows and columns with condition

I am working on a program to queue players of any video game of 4 players

this is my table structure:

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 |
   |
+-----------------+--------------+------+-----+-------------------+-------------

and this is the inserted data:

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

as you see user_priority has 2 values An for Active or current players Wn for waiting players n is the queue number of players list

Active players An from 1 to 4

waiting players Wn from 1 to no limit

the condition is that Active players are 2 teams

team 1: user_priority A1 and A2

team 2: user_priority A3 and A4

if team loses the game both players will move to the end of waiting list, and the firs 2 players in waiting list will move to the end of Active list and the winner team will move to the top on Active player list

for example team 2 loses so A3 and A4 will move to end of waiting list and the first 2 names of Waiting list will go to end of Active list

I'll just change the name of players and user_modifiedon

The main issue here is that I want to update (2 or 4 rows from Active players) and (everything in waiting list will be shifted) this is what I need!

because in this case I'll need to create an update statement for each row

+---------+-----------+---------------+-------------+---------------+-----------
----------+
| 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 |
+---------+-----------+---------------+-------------+---------------+-----------
----------+

I have create it a similar thing in java using 2 arraylists like this is the java code

These 2 arraylist will contain the list of Active and Waiting Players

 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);



    }

}

and this is the output

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]

my question is, what is the best SQL query to update multiple rows and keep the sequence correct as my example?

The main issue here is that I want to update (2 or 4 rows from Active players) and (everything in waiting list will be shifted) this is what I need!

thank you

If you're deleting W2 and need to shift all the higher ones down, you can do:

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%'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM