开发者社区> 问答> 正文

mysql执行insert前先select会报错?报错

我想把要插入的数据先查出来,同时把查出来的数据和一些常量一同插入表中,但是执行下面的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

请问这个问题怎么解决?

展开
收起
爱吃鱼的程序员 2020-06-08 16:50:44 783 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    语法问题,insetintoselect就可以了

    selectintofrom或者insertintoselect

    楼上正解,insertintoaselect*fromb

    2020-06-08 16:50:57
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
One Box: 解读事务与分析一体化数据库 HybridDB for MySQL 立即下载
One Box:解读事务与分析一体化数据库HybridDB for MySQL 立即下载
如何支撑HTAP场景-HybridDB for MySQL系统架构和技术演进 立即下载

相关镜像