从select语句向表中插入值

时间:2013-04-02 14:07:28

标签: mysql sql sql-insert

我有以下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。 我该怎么做?

2 个答案:

答案 0 :(得分:3)

您可以在values后指定selectinsert。在您的情况下,省略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