一尘不染

在MySQL中将数据从一个表插入到另一个表

mysql

我想从一个表中读取所有数据,然后将一些数据插入到另一个表中。我的查询是

  INSERT INTO mt_magazine_subscription ( 
      magazine_subscription_id, 
      subscription_name, 
      magazine_id, 
      status ) 
  VALUES ( 
      (SELECT magazine_subscription_id, 
              subscription_name, 
              magazine_id 
       FROM tbl_magazine_subscription 
       ORDER BY magazine_subscription_id ASC), '1')

但我有一个错误

  #1136 - Column count doesn't match value count at row 1

请帮我。


阅读 660

收藏
2020-05-17

共1个答案

一尘不染

您可以使用INSERT … SELECT语法。请注意,您可以在SELECT部分​​中直接引用“ 1”。

INSERT INTO mt_magazine_subscription ( 
      magazine_subscription_id, 
      subscription_name, 
      magazine_id, 
      status ) 
SELECT magazine_subscription_id, 
       subscription_name, 
       magazine_id, 
       '1'
FROM tbl_magazine_subscription
ORDER BY magazine_subscription_id ASC
2020-05-17