mysql执行insert前先select会报错

码上中国博客 发布于 2017/02/22 11:06
阅读 224
收藏 0

我想把要插入的数据先查出来,同时把查出来的数据和一些常量一同插入表中,但是执行下面的SQL会报错:

INSERT INTO usermsg_info(msgTitle,msgContent,createTime,sourceUserId,msgType,isRead,msgUrl,targetUserId)
VALUES(
   SELECT 
		c.msgTitle msgTitle, '第三次来,这次又不能成功' msgContent, 
		'2017-02-22 10:24:00' createTime, 3 sourceUserId, 
		3 msgType, '0' isRead, c.msgUrl, c.targetUserId
   FROM
     (SELECT a.blogTitle msgTitle,
             concat(concat(a.userId,'/'),concat(a.blogCategoryId,'/'),concat(a.fileName,'')) msgUrl,
             b.userid targetUserId
      FROM blog_info a,
           user_info b
      WHERE a.userId = b.userId
        AND a.id=29 
)c);

报错信息如下:

[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT 
		c.msgTitle msgTitle, '第三次来,这次又不能成功' msgConten' at line 3

但是单独执行要插入的那个select语句没问题:

   SELECT 
		c.msgTitle msgTitle, '第三次来,这次又不能成功' msgContent, 
		'2017-02-22 10:24:00' createTime, 3 sourceUserId, 
		3 msgType, '0' isRead, c.msgUrl, c.targetUserId
   FROM
     (SELECT a.blogTitle msgTitle,
             concat(concat(a.userId,'/'),concat(a.blogCategoryId,'/'),concat(a.fileName,'')) msgUrl,
             b.userid targetUserId
      FROM blog_info a,
           user_info b
      WHERE a.userId = b.userId
        AND a.id=29 
	)c

请问这个问题怎么解决?

加载中
0
高龄购物车驾驶员
高龄购物车驾驶员

语法问题,inset into select就可以了

0
Charkey
Charkey

select into from 或者 insert into select

0
风翔飞
风翔飞

楼上正解,insert into a select * from b

返回顶部
顶部