无法在同一个查询中运行 Insert 和 Select LAST_INSERT_ID()? [英] Can't run Insert and Select LAST_INSERT_ID() in the same query?

查看:57
本文介绍了无法在同一个查询中运行 Insert 和 Select LAST_INSERT_ID()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 node 连接到 mysql,我需要运行一个插入,然后立即运行一个 select last_insert_id().

insert into data_temp values (null, '{"test":{"id":12,"otherdata":"x","otherdata2":"y"}}');选择 LAST_INSERT_ID();

在 mysql 工作台中,此查询有效但给我一个错误说:

SELECT LAST_INSERT_ID() )

<块引用>

错误代码:1064.您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册在第 2 行 ')' 附近使用的语法 0.047 秒

它实际上返回了正确的 id,但是因为它在最后一部分节点上抛出了一个错误,所以它正在捕获错误.

解决方案

插入查询中存在一个问题,您刚刚错过了输入列的名称

这样做

insert into data_temp (`col1`, `col2`) 值 (null, '{"test":{"id":12,"otherdata":"x","otherdata2":"y"}}');选择 LAST_INSERT_ID();

代替 col1col2 输入您的列名称,然后它将为您工作.

I am using node to connect to mysql and I need to run an insert and then immediately run a select last_insert_id().

insert into data_temp values (null, '{"test":{"id":12,"otherdata":"x","otherdata2":"y"}}');
SELECT LAST_INSERT_ID();

In mysql workbench this query works but gives me an error saying:

SELECT LAST_INSERT_ID() )

Error Code: 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 ')' at line 2 0.047 sec

It actually returns the correct id but because it is throwing an error on the last part node is catching the error.

解决方案

There is an issue in your insert query you just missed to put the name of your columns

Do like this

insert into data_temp (`col1`, `col2`) values (null, '{"test":
{"id":12,"otherdata":"x","otherdata2":"y"}}'); SELECT LAST_INSERT_ID();

In place of col1and col2 put your columns name then it will work for you.

这篇关于无法在同一个查询中运行 Insert 和 Select LAST_INSERT_ID()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆