使用Select&插入同一查询

时间:2012-09-13 15:12:29

标签: mysql

我有三个表,我正在尝试从一个表中执行SELECT而在另一个表中执行SELECT操作,并在具有相同值的三个列上使用ON。

这就是我想要的......

INSERT INTO
CustomerDetails_TEST
(Region_ID)

SELECT
RegionStatesUS.Region_ID

LEFT JOIN Customers ON Customers.Customer_ID = CustomerDetails_TEST.Customer_ID
LEFT JOIN RegionStatesUS ON RegionStatesUS.RegionState = Customers.BillingStateOrProv

FROM Customers
WHERE Customers.Customer_ID = CustomerDetails_TEST.Customer_ID

表格是......

CustomerDetails_TEST
RegionStatesUS
Customers

我正在尝试INSERT使用RegionStatesUS.Region_ID Customer_ID INTOCustomerDetails_TEST Region_ID的价值。{{1}}

我在这里缺少什么?

3 个答案:

答案 0 :(得分:1)

FROM来自LEFT JOIN。错误消息暗示了这一点。再试一次。

答案 1 :(得分:1)

你的SQL语法错了,我想你可能想要这个:

INSERT INTO CustomerDetails_TEST (Region_ID)
SELECT r.Region_ID
FROM Customers c1
LEFT JOIN CustomerDetails_TEST c2
    ON c1.Customer_ID = c2.Customer_ID
LEFT JOIN RegionStatesUS r
    ON c1.BillingStateOrProv = r.RegionState 

更新查询类似于此未测试

UPDATE CustomerDetails_TEST c1
LEFT JOIN Customers c2
    ON c1.Customer_ID = c2.Customer_ID
LEFT JOIN RegionStatesUS r
    ON c2.BillingStateOrProv = r.RegionState 
SET c1.Region_ID = r.Region_ID

答案 2 :(得分:0)

无法看到评论的位置 只是关于如何更新的说明,或

insert into mytable
select from sometable
on duplicate key update
id = sometable.id;