这个子查询有什么问题

时间:2017-03-27 13:07:47

标签: mysql

我正在尝试运行子查询,以便从另一个数据库的表中收集的数据将数据插入到数据库的表中。我运行此查询时收到以下错误:

  

1064 - 您的SQL语法出错;检查与MariaDB服务器版本对应的手册,以获得正确的语法

     

靠近'SELECT * FROM campaign_traffic_db.campaign_merge WHERE   第1行的adform_clientID = 135456'

INSERT
INTO
  135456_1257994024297010.campaign_merge(
    adform_client,
    adform_clientID,
    adform_campaign,
    adform_campaignID,
    adform_campaignStartDate,
    adform_campaignEndDate,
    adform_campaignCurrency,
    adform_cost,
    adform_clicks,
    adform_ctr,
    adform_impression,
    fb_client,
    fb_clientID,
    fb_campaign,
    fb_campaignID,
    fb_campaignStartDate,
    fb_campaignEndDate,
    fb_cost,
    fb_clicks,
    fb_impression,
    created_at,
    updated_at
  )
VALUES (
SELECT
  *
FROM
  campaign_traffic_db.campaign_merge
WHERE
  adform_clientID = '135456' AND fb_clientID = '1257994024297010')

1 个答案:

答案 0 :(得分:0)

作为上述评论的后续内容,语法为:

INSERT INTO <table> (f1, f2, f3)
VALUES (SELECT f1, f2, f3 FROM <table2>);

不行。您可以使用VALUES()或提供SELECT语句,该语句返回所需的字段以匹配您的INSERT字段列表。

INSERT INTO 135456_1257994024297010.campaign_merge
(
    adform_client,
    adform_clientID,
    adform_campaign,
    adform_campaignID,
    adform_campaignStartDate,
    adform_campaignEndDate,
    adform_campaignCurrency,
    adform_cost,
    adform_clicks,
    adform_ctr,
    adform_impression,
    fb_client,
    fb_clientID,
    fb_campaign,
    fb_campaignID,
    fb_campaignStartDate,
    fb_campaignEndDate,
    fb_cost,
    fb_clicks,
    fb_impression,
    created_at,
    updated_at
)
SELECT * FROM campaign_traffic_db.campaign_merge
WHERE adform_clientID = '135456' AND fb_clientID = '1257994024297010';

应该有效,假设campaign_traffic_db.campaign_merge中的字段与语句的INSERT部分中的字段列表完全对齐。

可以找到有关INSERT ... SELECT的文档here