使用SELECT从嵌套的INSERT语句中提取数据?

时间:2021-08-27 09:52:50

Is it possible to have an INSERT statement return the columns affected into a SELECT statement? For example, I have the statement:

是否可以让INSERT语句将受影响的列返回到SELECT语句中?例如,我有声明:

INSERT INTO work_day (WorkDateId, TimeframeId) VALUES (@selecteddateid,@timeframeid);

But work_day has an auto incrementing, work_dayId, that gets created when this row data is inserted. I want to put this work_dayId in another statement, but I was wondering if it would be able to nest this INSERT inside a SELECT that will select the affected/created row.

但是work_day有一个自动递增的work_dayId,它会在插入此行数据时创建。我想把这个work_dayId放在另一个语句中,但我想知道它是否能够将这个INSERT嵌套在SELECT中,该SELECT将选择受影响/创建的行。

So would I be able to place this statement like so:

那么我能够像这样发表这样的声明:

INSERT INTO appointment 
(customerid, WorkDayId, UserId, Priority, Assign)
VALUES 
(@otherdata
(SELECT WorkDayId FROM work_day WHERE WorkDateId = (INSERT INTO work_day (WorkDateId, TimeframeId) VALUES (@selecteddateid,@timeframeid))));

1 个解决方案

#1


0  

AS @Gordon stated:

AS @Gordon说:

No, you cannot do that. You want last_insert_id()

不,你做不到。你想要last_insert_id()

Posting it here as an answer for better visibility.

在此发布它作为更好的可见性的答案。

#1


0  

AS @Gordon stated:

AS @Gordon说:

No, you cannot do that. You want last_insert_id()

不,你做不到。你想要last_insert_id()

Posting it here as an answer for better visibility.

在此发布它作为更好的可见性的答案。