简体   繁体   中英

Insert from two tables where condition

I have 3 tables

Table 1 EIN columns :

id, name, plate,in_datetime,time,image-name.

Table 2 EOUT columns :

id, name, plate, out_datetime, time, image-name.

Table 3 recon columns :

id, plate,in_datetime,in_entry_id,out_datetime, out_entry_id,Processed, duration.

I need to get table 1 and table 2 values into table 3 columns using where condition. I am using the below query:

INSERT INTO recon (id,EIN.plate,EIN.in_datetime, EIN.id,  EOUT.out_datetime, EOUT.id,null, null) 
    SELECT EIN.RegistrationMark,EIN.datetime,EIN.id, EOUT.date,EOUT.id FROM EIN_anpr_vega as EIN, EOUT_anpr_vega as  EOUT
    where EIN.plate = EOUT.plate
    and EIN.in_datetime = EOUT.out_datetime

I am getting an error near null, null. below is the error.

for the right syntax to use near 'null, null

In your column names you cant write null

INSERT INTO recon (id,EIN.plate,EIN.in_datetime, EIN.id,  EOUT.out_datetime, EOUT.id,null, null) 

Instead, write the column names and place the nulls in the select. Like this:

INSERT INTO recon (id,EIN.plate,EIN.in_datetime, EIN.id,  EOUT.out_datetime, EOUT.id,col1, col2) 
    SELECT EIN.RegistrationMark,EIN.datetime,EIN.id, EOUT.date,EOUT.id, null, null FROM EarlsdonMSIN_anpr_vega as EIN, EarlsdonMSOUT_anpr_vega as  EOUT
    where EIN.plate = EOUT.plate
    and EIN.in_datetime = EOUT.out_datetime
INSERT INTO recon (id, EIN.plate ...) VALUES ...

The things inside the first () are supposed to be column names for the table recon . (I'm surprised that it did not show a syntax error with EIN.plate .)

Yes, later, null will get into trouble for the same reason -- it is not a column name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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