project user
1 1
1 2
2 1
2 2
3 1
3 2
3 3
从以上记录可以看到user=3的值,仅出现在了project=3的记录中,我现在希望在project=1 和 2中,批量的将user=3也加入进去。sql语句该如何写?最终执行查询排序后的结果应该如下:
project user
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
请注意: 红色字体的记录就是我希望根据一定条件批量插入的记录。
4 个解决方案
#1
放到同一张表中 project_user表不是有project =1和2的记录了吗 干吗还插入
insert into project_user select * from project_user where project=1 or 2
#2
INSERT INTO tta
SELECT c.`project`,c.`user` FROM tta b RIGHT JOIN (
SELECT * FROM
(SELECT DISTINCT a.`project` FROM tta a) a1,
(SELECT DISTINCT a.`user` FROM tta a) b1) c
ON b.`user`=c.`user` AND b.`project`=c.`project` WHERE b.`project` IS NULL;
SELECT c.`project`,c.`user` FROM tta b RIGHT JOIN (
SELECT * FROM
(SELECT DISTINCT a.`project` FROM tta a) a1,
(SELECT DISTINCT a.`user` FROM tta a) b1) c
ON b.`user`=c.`user` AND b.`project`=c.`project` WHERE b.`project` IS NULL;
#3
学到了,谢谢
#4
以下是我自己想出来的办法:
insert into `project_user` (project,user,)
SELECT distinct project,3 FROM `project_user` where project not in(
SELECT project FROM `project_user` where user = '3')
insert into `project_user` (project,user,)
SELECT distinct project,3 FROM `project_user` where project not in(
SELECT project FROM `project_user` where user = '3')
#1
放到同一张表中 project_user表不是有project =1和2的记录了吗 干吗还插入
insert into project_user select * from project_user where project=1 or 2
#2
INSERT INTO tta
SELECT c.`project`,c.`user` FROM tta b RIGHT JOIN (
SELECT * FROM
(SELECT DISTINCT a.`project` FROM tta a) a1,
(SELECT DISTINCT a.`user` FROM tta a) b1) c
ON b.`user`=c.`user` AND b.`project`=c.`project` WHERE b.`project` IS NULL;
SELECT c.`project`,c.`user` FROM tta b RIGHT JOIN (
SELECT * FROM
(SELECT DISTINCT a.`project` FROM tta a) a1,
(SELECT DISTINCT a.`user` FROM tta a) b1) c
ON b.`user`=c.`user` AND b.`project`=c.`project` WHERE b.`project` IS NULL;
#3
学到了,谢谢
#4
以下是我自己想出来的办法:
insert into `project_user` (project,user,)
SELECT distinct project,3 FROM `project_user` where project not in(
SELECT project FROM `project_user` where user = '3')
insert into `project_user` (project,user,)
SELECT distinct project,3 FROM `project_user` where project not in(
SELECT project FROM `project_user` where user = '3')