SQL批量查询之后进行批量更新或插入。
UPDATEusers_metadataSETMetaValue='true'WHEREUserIdIN(SELECTDISTINCTum.UserIdFROMcente...
UPDATE users_metadata SET MetaValue = 'true' WHERE UserId IN (
SELECT DISTINCT um.UserId FROM centers_metadata cm
INNER JOIN contents_group cg ON cm.CenterId = cg.CenterId
INNER JOIN contents_usergroup cu ON cu.GroupId = cg.Id
INNER JOIN users_metadata um ON um.UserId = cu.UserId
WHERE cm.MetaKey = 'ACADEMY_OPEN_FLAG'
AND cm.MetaValue = 'true'
AND um.MetaKey = 'ACADEMY_OPEN_FLAG'
AND um.MetaValue = 'false'
上面是已做的,下面是需求。上面的肯定很烂,优化一下也不错。。谢谢(Sql Server)
我是做java的实在不行,就用java写了。
//contents_usergroup 表跟users_metadata 表没有关联上的UserId,进行下面的插入,多条
INSERT INTO users_metadata (MetaKey, MetaValue, UserId) VALUES ('ACADEMY_OPEN_FLAG','true','')
查询未关联成功的UserId
SELECT DISTINCT cu.UserId FROM centers_metadata cm
INNER JOIN contents_group cg ON cm.CenterId = cg.CenterId
INNER JOIN contents_usergroup cu ON cu.GroupId = cg.Id
WHERE cm.MetaKey = 'ACADEMY_OPEN_FLAG'
AND cm.MetaValue = 'true'
AND cu.UserId
NOT IN (SELECT UserId FROM users_metadata WHERE MetaKey = 'ACADEMY_OPEN_FLAG')
现在就是把上面这条sql查到的内容,批量添加
INSERT INTO users_metadata (MetaKey, MetaValue, UserId) VALUES ('ACADEMY_OPEN_FLAG','true',UserId) 展开
SELECT DISTINCT um.UserId FROM centers_metadata cm
INNER JOIN contents_group cg ON cm.CenterId = cg.CenterId
INNER JOIN contents_usergroup cu ON cu.GroupId = cg.Id
INNER JOIN users_metadata um ON um.UserId = cu.UserId
WHERE cm.MetaKey = 'ACADEMY_OPEN_FLAG'
AND cm.MetaValue = 'true'
AND um.MetaKey = 'ACADEMY_OPEN_FLAG'
AND um.MetaValue = 'false'
上面是已做的,下面是需求。上面的肯定很烂,优化一下也不错。。谢谢(Sql Server)
我是做java的实在不行,就用java写了。
//contents_usergroup 表跟users_metadata 表没有关联上的UserId,进行下面的插入,多条
INSERT INTO users_metadata (MetaKey, MetaValue, UserId) VALUES ('ACADEMY_OPEN_FLAG','true','')
查询未关联成功的UserId
SELECT DISTINCT cu.UserId FROM centers_metadata cm
INNER JOIN contents_group cg ON cm.CenterId = cg.CenterId
INNER JOIN contents_usergroup cu ON cu.GroupId = cg.Id
WHERE cm.MetaKey = 'ACADEMY_OPEN_FLAG'
AND cm.MetaValue = 'true'
AND cu.UserId
NOT IN (SELECT UserId FROM users_metadata WHERE MetaKey = 'ACADEMY_OPEN_FLAG')
现在就是把上面这条sql查到的内容,批量添加
INSERT INTO users_metadata (MetaKey, MetaValue, UserId) VALUES ('ACADEMY_OPEN_FLAG','true',UserId) 展开
1个回答
展开全部
果然是很乱。还没看懂你需求。
不如你说清楚题目需求吧
不如你说清楚题目需求吧
追问
INSERT INTO users_metadata (MetaKey, MetaValue, UserId) VALUES ('ACADEMY_OPEN_FLAG','true',这里是我查到的一堆UserID)
做一个批量插入就行了。问题已补充
追答
INSERT INTO users_metadata (MetaKey, MetaValue, UserId)
select 'ACADEMY_OPEN_FLAG','true',321 union
select 'ACADEMY_OPEN_FLAG','true',123
union 加个这关键字可以做到对一个表的批量插入数据
用法基本你上面的语法差不多 就换了一些关键字而已小括号没有而已
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询