我有以下SQL查询:
INSERT INTO site_city_address (`cityName`, `cityCode`, `stateId`) VALUES (SELECT a.city, a.cityCode, b.stateId
FROM site_address_dropdown a INNER JOIN site_state_address b
ON a.state = b.stateName);
我知道这可能是错的。我想要做的是将a.city,a.cityCode和b.stateId的值作为cityName,cityCode和stateId插入shopious_city_address。 我该怎么做?
答案 0 :(得分:3)
您可以在values
后指定select
或insert
。在您的情况下,省略values
:
INSERT INTO site_city_address
(`cityName`, `cityCode`, `stateId`)
SELECT a.city, a.cityCode, b.stateId
FROM site_address_dropdown a
INNER JOIN
site_state_address b
ON a.state = b.stateName;
答案 1 :(得分:1)
删除VALUES
关键字。 INSERT INTO...SELECT
具有以下语法
INSERT INTO site_city_address (cityName, cityCode, stateId)
SELECT a.city, a.cityCode, b.stateId
FROM site_address_dropdown a
INNER JOIN site_state_address b
ON a.state = b.stateName