-1

我有三个表,我正在尝试从一个表中执行 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 INTO CustomerDetails_TESTRegion_ID

我在这里想念什么?

4

3 回答 3

1

FROM 出现在 LEFT JOIN 之前。错误消息暗示了这一点。再试一次。

于 2012-09-13T15:39:44.133 回答
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
于 2012-09-13T15:43:28.013 回答
0

看不到在哪里评论只是关于如何更新的注释,或者

insert into mytable
select from sometable
on duplicate key update
id = sometable.id;
于 2012-09-20T06:37:10.380 回答