简体   繁体   中英

Insert into two different databases

I am trying to insert into two different databases using MySQL;-

`$this->sql = "INSERT INTO tblfixedfare SELECT NULL, MAX(FixedFareID)+1, '1', '$fieldDay' FROM tblfixedfare UNION SELECT NULL, MAX(FixedFareID)+1, '2', '$fieldNight' FROM tblfixedfare,INSERT INTO tblfixedfaresetup SELECT NULL, MAX(FixedFareID)+1, '$fieldFrompc', '1', '2' FROM tblfixedfaresetup UNION SELECT NULL, MAX(FixedFareID)+1, '$fieldTopc', '2', '2' FROM tblfixedfaresetup";`

There is not a problem with each insert, as both insert two rows into each table individually.

How would I get both of these to execute at the same time? At the moment I get the following error:

Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near`

'INSERT INTO tblfixedfaresetup SELECT NULL, MAX(FixedFareID)+1, 'DE115', '1', '2'' at line 2SQL: INSERT INTO tblfixedfare SELECT NULL, MAX(FixedFareID)+1, '1', '20' FROM tblfixedfare UNION SELECT NULL, MAX(FixedFareID)+1, '2', '30' FROM tblfixedfare UNION
                                INSERT INTO tblfixedfaresetup SELECT NULL, MAX(FixedFareID)+1, 'DE115', '1', '2' FROM tblfixedfaresetup UNION SELECT NULL, MAX(FixedFareID)+1, 'DE116', '2', '2' FROM tblfixedfaresetup`

you can use two different connection and separate insert query to insert in two database. or use databasename.table name when insert. Thanks

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