在mysql语句中INSERT INTO和SELECT多个值

时间:2015-03-16 10:37:43

标签: mysql

我无法正确使用mysql语法。 我想在同一个数据库和今天的日期CURDATE()中从另一个表中插入2个数据post_idmeta_value

INSERT into wp_top_voted  

post_idnumber_votestodays_date

以下内容:

SELECT `post_id`  
FROM  `wp_postmeta` WHERE  `meta_key` =  'votes' ORDER BY  `meta_value` DESC   
LIMIT 10
SELECT `meta_value`  
FROM  `wp_postmeta` WHERE  `meta_key` =  'votes' ORDER BY  `meta_value` DESC   
LIMIT 10
CURDATE()) 

如何将上述内容合并为一个参数?我试过了

INSERT into wp_top_voted  
(`post_id`, `number_votes`, `todays_date`)  
values (SELECT `post_id`  
FROM  `wp_postmeta` WHERE  `meta_key` =  'votes' ORDER BY  `meta_value` DESC   
LIMIT 10, SELECT `meta_value`  
FROM  `wp_postmeta` WHERE  `meta_key` =  'votes' ORDER BY  `meta_value` DESC   
LIMIT 10,   CURDATE()) 

但我收到错误。

2 个答案:

答案 0 :(得分:1)

你可以在这里找到一个很好的解释http://www.w3schools.com/sql/sql_insert_into_select.asp

您需要INSERT INTO SELECT语句

INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Suppliers;

您的具体情况应该是:

INSERT into wp_top_voted ('post_id','meta_value','todays_date' ) SELECT 'post_id' ,'meta_value' ,CURDATE()
FROM  'wp_postmeta' WHERE  'meta_key' =  'votes' ORDER BY 'meta_value' DESC   
LIMIT 10

答案 1 :(得分:1)

由于INSERT INTO TABLE() SELECT的格式不正确,您收到错误。 试试这个:

INSERT into wp_top_voted  
(`post_id`, `number_votes`, `todays_date`)  
SELECT `post_id`, `meta_value`, CURDATE() 
FROM  `wp_postmeta` WHERE  `meta_key` =  'votes' ORDER BY  `meta_value` DESC   
LIMIT 10;