[英]SQL Query, insert a max value from another table's column
I have two tables, a booking table and an invoice table. 我有两个表,一个预订表和一个发票表。 I am trying to update the booking table with booking information and get a max value from the invoice table and insert it into the booking table at the same time.
我正在尝试使用预订信息更新预订表,并从发票表中获取最大值并将其同时插入到预订表中。
So far I have this, but it doesn't set any values to the Booking.Invoice_id
column 到目前为止,我已经有了这个,但是它没有为
Booking.Invoice_id
列设置任何值
INSERT INTO Booking( user_id, Location_id, Accom_Id,StartDate,EndDate,
Vehreg,PartySize,Invoice_id )
VALUES ('$User_id', '$pitch', '$Accom' , '$start',
'$end','$Vreg','$guests','SELECT Max Invoice_id FROM Invoice;');
any help would be much appreciated 任何帮助将非常感激
Use insert . . . select
使用
insert . . . select
insert . . . select
insert . . . select
: insert . . . select
:
INSERT INTO Booking(user_id, Location_id, Accom_Id, StartDate, EndDate,
Vehreg, PartySize, Invoice_id )
SELECT '$User_id', '$pitch', '$Accom' , '$start',
'$end', '$Vreg', ' $guests',
MAX(Invoice_ID)
FROM Invoice;
My guess, however, is that you want to run this immediately after inserting a row into Invoice
. 但是,我的猜测是,您要在向
Invoice
插入一行后立即运行它。 In that case, you should be using LAST_INSERT_ID()
. 在这种情况下,您应该使用
LAST_INSERT_ID()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.