使用特定列名将MySQL表值复制到重复表

时间:2016-02-24 16:46:56

标签: mysql

我试图将表格的值复制到一个新表格中,这样我就可以制作ALTER语句,而不会在几天内锁定表格。

但是,我还想对表列结构进行一些更改。所以我不想使用*

相反,我希望能够像普通的insert语句一样指定列。 但由于某种原因,它不喜欢这种语法?

INSERT INTO earnings_new 
(earning_account_id, earning_link_id, earning_ticket_id, earning_referral_id, 
earning_referral_type, earning_ad_id, earning_verified, earning_paid, earning_amount, earning_country,
earning_country_id, earning_ip, earning_hour, earning_day, earning_month, earning_year, earning_created, earning_updated) 
VALUES 
(
SELECT
earning_account_id, earning_link_id, earning_ticket_id, earning_referral_id, 
earning_referral_type, earning_ad_id, earning_verified, earning_paid, earning_amount, earning_country,
earning_country_id, earning_ip, earning_hour, earning_day, earning_month, earning_year, earning_created, earning_updated
FROM earnings
WHERE earning_month = 2 AND earning_day = 12 AND earning_hour = 13
GROUP BY earning_id
ORDER BY earning_created DESC
)

2 个答案:

答案 0 :(得分:1)

尝试省略VALUES关键字。

INSERT INTO earnings_new 
(earning_account_id, earning_link_id, earning_ticket_id, earning_referral_id, 
earning_referral_type, earning_ad_id, earning_verified, earning_paid, earning_amount, earning_country,
earning_country_id, earning_ip, earning_hour, earning_day, earning_month, earning_year, earning_created, earning_updated) 

(
SELECT
earning_account_id, earning_link_id, earning_ticket_id, earning_referral_id, 
earning_referral_type, earning_ad_id, earning_verified, earning_paid, earning_amount, earning_country,
earning_country_id, earning_ip, earning_hour, earning_day, earning_month, earning_year, earning_created, earning_updated
FROM earnings
WHERE earning_month = 2 AND earning_day = 12 AND earning_hour = 13
GROUP BY earning_id
ORDER BY earning_created DESC
)

Reference

答案 1 :(得分:1)

删除VALUES字。它在MySql中不需要

   INSERT INTO earnings_new
(earning_account_id, earning_link_id, earning_ticket_id, earning_referral_id, 
earning_referral_type, earning_ad_id, earning_verified, earning_paid, earning_amount, earning_country,
earning_country_id, earning_ip, earning_hour, earning_day, earning_month, earning_year, earning_created, earning_updated) 

SELECT
earning_account_id, earning_link_id, earning_ticket_id, earning_referral_id, 
earning_referral_type, earning_ad_id, earning_verified, earning_paid, earning_amount, earning_country,
earning_country_id, earning_ip, earning_hour, earning_day, earning_month, earning_year, earning_created, earning_updated
FROM earnings
WHERE earning_month = 2 AND earning_day = 12 AND earning_hour = 13
GROUP BY earning_id
ORDER BY earning_created DESC