简体   繁体   English

执行查询语句后从SQL获取Row数据

[英]Getting the Row data from the SQL after executing the query statement

I have a MYSQL statement which inserts data into a row. 我有一个MYSQL语句,它将数据插入一行。 The row id would be automatically incremented. 行id将自动递增。

But how do I get back that row of data which got inserted and executed? 但是如何找回插入并执行的那一行数据呢? That row of data should includes the row id which I no idea of what that is.. 那行数据应该包含行id,我不知道那是什么..

I want the whole row $row, not just the id. 我想要整行$ row,而不仅仅是id。 I don't know the id because it's autoincremented. 我不知道id,因为它是自动增量的。

Since you have an autoincremented id you can use LAST_INSERT_ID() 由于您具有自动增量ID,因此可以使用LAST_INSERT_ID()

Eg 例如

INSERT INTO supportContacts
(type, details)
VALUES
('Twitter', '@sqlfiddle');

SELECT * FROM 
supportContacts 
WHERE id = LAST_INSERT_ID();

DEMO DEMO

If you have an auto-incrementing column for the id, the following will work in any database: 如果您有id的自动递增列,则以下内容适用于任何数据库:

select *
from table
where id = (select max(id) from table)

Under the assumption that no one else is inserting rows. 假设没有其他人插入行。

In SQL Server, you can do the following: 在SQL Server中,您可以执行以下操作:

declare @id int;

insert . . .

select @id = @@Identity;

select *
from table
where id = @id;

Just note that the line with @@Identity needs to follow immediately after the insert. 请注意,插入后需要立即跟随带@@ Identity的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM