0

I am trying to write an sql script to do a bulk insert. I need it to add the users that are managers into the manager's group. I tried to write it like this

INSERT INTO group_member (group_id, user_id) VALUES ((SELECT group_id FROM user_group WHERE group_name = 'Manager') , (SELECT user_id 
FROM user WHERE manager=1 and user_status = 1));

but I am getting this error

Subquery returns more than 1 row

I understand the error but am not sure how to work around it so that I do not miss any users.

When run there can be 0 to many managers, not sure if that will make a difference.

sql version: 5.6.27

CREATE TABLE user_group(
    group_id INT(11) NOT NULL AUTO_INCREMENT,
    group_name VARCHAR(128) NULL DEFAULT NULL,
    PRIMARY KEY (group_id)
);

CREATE TABLE user (
    user_id INT(11) NOT NULL AUTO_INCREMENT,
    user_name VARCHAR(128) NULL DEFAULT NULL,
    manager INT(11) NOT NULL
    user_status INT(11) NOT NULL
    PRIMARY KEY (user_id)
);

CREATE TABLE group_member (
    group_id INT(11) NOT NULL,
    user_id INT(11) NOT NULL,
    PRIMARY KEY (group_id, user_id)
);
3
  • can you please post the table definitions please Commented Apr 30, 2018 at 20:49
  • which sql version? Commented Apr 30, 2018 at 20:49
  • @maSTAShuFu updated Commented Apr 30, 2018 at 21:10

2 Answers 2

1

You want insert . . . select:

INSERT INTO group_member (group_id, user_id)
    SELECT g.group_id, u.user_id
    FROM (SELECT group_id FROM user_group WHERE group_name = 'Manager') g CROSS JOIN
         (SELECT user_id FROM user WHERE manager = 1 and user_status = 1) u;

If the group already has members, you might want to filter them out.

You can also write this as:

INSERT INTO group_member (group_id, user_id)
    SELECT g.group_id, u.user_id
    FROM user u JOIN
         user_group g
         ON g.group_name = 'Manager' AND
            (u.manager = 1 and u.user_status = 1);
Sign up to request clarification or add additional context in comments.

5 Comments

@Daffy13 I'm a bit surprised. On my 5.7.21 MySQL, both versions result in a syntax error (ERROR 1064 (42000)) due to the word "group" not being escaped. The MySQL manual page even lists GROUP as a forbidden name (dev.mysql.com/doc/refman/5.6/en/keywords.html). Have you set some special configuration option?
my real table is named user_group, so it worked for me
@LSerni should I update the question with that info. I was not thinking it was going to matter at first
@Daffy13 yes, you definitely should. I wonder why my code did not work though: with this modification, it should have from the start. Was that the only difference? You should frame your question so that it follows your code as close as possible, otherwise you might receive nonworking - or even harmful - answers. That's was why maSTAShuFu asked for your table definitions.
@LSerni my guess is that I messed something up. it is working for me now
1

I need it to add the users that are managers into the manager's group

INSERT INTO group_member (group_id, user_id)
SELECT (SELECT group_id FROM `group` WHERE group_name = 'Manager'),
       user_id 
FROM user WHERE manager=1 and user_status = 1;

Of course, there must be only one group with the name of Manager.

Test

mysql> SELECT * FROM `user`;
+---------+-------------+---------+
| manager | user_status | user_id |
+---------+-------------+---------+
|       1 |           1 |       1 |
|       1 |           1 |       2 |
+---------+-------------+---------+
2 rows in set (0.00 sec)

mysql> SELECT * FROM `group`;
+----------+------------+
| group_id | group_name |
+----------+------------+
|       17 | Manager    |
+----------+------------+
1 row in set (0.00 sec)

mysql> INSERT INTO group_member (group_id, user_id)
    ->     SELECT (SELECT group_id FROM `group` WHERE group_name = 'Manager'),
    ->            user_id
    ->     FROM user WHERE manager=1 and user_status = 1;
Query OK, 2 rows affected (0.08 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * FROM group_member;
+----------+---------+
| group_id | user_id |
+----------+---------+
|       17 |       1 |
|       17 |       2 |
+----------+---------+
2 rows in set (0.00 sec)

1 Comment

@Daffy13 you're correct: group is a keyword and should have been escaped. Amended answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.