简体   繁体   English

SQL查询,从另一个表的列中插入最大值

[英]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.

相关问题 SQL从一个表列中选择一个最小值,然后将结果插入一个SQL语句的另一个表列中 - Sql Select a minimum value from a table column and insert the results in another table column in one SQL statement 从另一张表中查询两列值的SQL查询 - SQL query for two column value print from another table SQL查询以获取与另一列的MAX值对应的列值? - SQL Query to get column values that correspond with MAX value of another column? SQL根据来自另一个相关表中的列的相应最小值/最大值(值)获取值 - SQL get value based on corresponding min/max(value) from column in another related table SQL:自动插入另一个表中的值 - SQL: Auto insert the a value from another table mysql查询插入一列的值相对于同一表中的另一列 - mysql query to insert the value of a column with respect to another column in same table SQL数据库 - 为另一个表列获取一列最大值 - SQL Database - Taking one columns max value for another table column MySQL INSERT INTO查询列值等于另一个SQL SELECT查询 - MySQL INSERT INTO query column value equals another SQL SELECT query 从SQL表中选择MAX值以应用于另一个表 - Select MAX value from SQL table to apply in another table 根据另一列的值将值插入新表的列中 - Insert value into column in new table based on value from another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM