我想知道是否可以从两个不同的表中插入数据(通过选择我想要的列)并将它们插入到新表中。
这是我的查询,它似乎不起作用。
INSERT INTO book2.orders_bought (`id`, `sold_order_id`, `sold_order_item_id`, `item_id`,
`status`, `card_name`, `supplier_name`, `total`, `date_ordered`)
SELECT `order_id`, `bought_order_id`, `oi.id`, SUBSTR(sell_link, 97, 20), `status`,
`card_name`, `supplier_name`, `total`, `date`
FROM book1.orders_buy, book1.order_items oi
注意:我想从表order_items中提取的唯一字段是id
答案 0 :(得分:1)
INSERT INTO book2.orders_bought (
`id`, `sold_order_id`, `sold_order_item_id`, `item_id`,
`status`, `card_name`, `supplier_name`, `total`, `date_ordered`)
SELECT
`b.order_id`, `bought_order_id`, `oi.id`, SUBSTR(sell_link, 97, 20),
`status`, `card_name`, `supplier_name`, `total`, `date`
FROM book1.orders_buy b
LEFT JOIN book1.order_items oi ON oi.order_id = b.order_id