i hava a table with varchar primary key, that is a foreing key for some other tables.
Something like:
ID-------------NAME
1011001020-----product 1
1011001022-----product 2
1011001025-----product 3
Then, i have this array
array(
'1011001020',
'1011001022',
'1011001025',
'x',
'y'
)
This array will be used to insert values in another table with FK, so if any value is not an ID on the first table, the INSERT query will brake.
How do i find 'x' and 'y' before any attempt to insert. I would like to avoid selecting all ids from table one and make a PHP comparison, since there are a lot of records. I would rather a MySQL approach
I would suggest the following procedure:
The second and third steps can be done easily in SQL:
delete from temporarytable
where tt.otherid not in (select ot.otherid from othertable ot);
alter table temporarytable
add constraint fk_othertable foreign key (otherid) references othertable(otherid);
You can load the data however you like. For speed, I would recommend load data infile
.
give this a shot. Tested on server, products is the new table and inserted only rows found in the array from items table.
INSERT INTO `products` ( `name` )
SELECT `name` FROM `items` WHERE `vendor_id` IN ( 1,2,3,4,.... )
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.