'mysqli Insert from multiple table
I want to insert into mysqli from two table like this
INSERT INTO oc_lts_plan(vendor_id, subscription_id, name, no_of_product, join_fee, subscription_fee, validity, date_added, date_expire)
SELECT lv.vendor_id
FROM oc_lts_vendor lv
UNION ALL
SELECT ls.subscription_id, lsd.name, ls.no_of_product, ls.join_fee, ls.subscription_fee, ls.validity, now(), (DATE_ADD(now(), INTERVAL 1 month)
FROM oc_lts_subscription ls
LEFT JOIN oc_lts_subscription_description lsd ON ls.subscription_id=lsd.subscription_id
WHERE ls.default_plan='1'
the error: The used SELECT statements have a different number of columns
Solution 1:[1]
your second select has no match for the vendor_id column, if the vendor_id column is autogenerate, in this case, you can remove it on the first select
INSERT INTO oc_lts_plan(subscription_id, name, no_of_product, join_fee, subscription_fee, validity, date_added, date_expire)
SELECT lv.vendor_id
FROM oc_lts_vendor lv
UNION ALL
SELECT ls.subscription_id, lsd.name, ls.no_of_product, ls.join_fee, ls.subscription_fee, ls.validity, now(), (DATE_ADD(now(), INTERVAL 1 month)
FROM oc_lts_subscription ls
LEFT JOIN oc_lts_subscription_description lsd ON ls.subscription_id=lsd.subscription_id
WHERE ls.default_plan='1'
If your primary key is both (vendor_id, subscription_id), you will have no choice but to do the same on your second select, and that will depend on the structure of your second table
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
