从几个表中抓取数据并插入新表

时间:2016-05-06 22:25:40

标签: mysql

我想将以下语句中的结果数据作为现有表中的新行插入:

SELECT a.post_date, b.vendor_name, c.lastName, d.firstName, null as taxiGroup
FROM (select ID,post_date from wp_posts where post_type = 'shop_order') as a, 
(SELECT order_id,vendor_name FROM wp_wcpv_commissions) as b,
(SELECT post_id,meta_value as lastName  FROM wp_postmeta  where  meta_key ='_billing_last_name') as c, 
(SELECT post_id,meta_value as firstName FROM wp_postmeta WHERE  meta_key ='_billing_first_name') as d
WHERE a.ID = b.order_id and b.order_id=c.post_id and c.post_id = d.post_id;

但是,如果我使用INSERT INTO taxipassengers () VALUES(<the query above>);形式,我会得到You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 6,无论那是什么意思。

建议?

编辑:乍一看,这是一个双引号,但在我复制/粘贴之后,我看到它是两个连续的单引号,这是仍然很好奇,但至少更有意义,因为我在查询中的任何地方都没有双引号。

2 个答案:

答案 0 :(得分:1)

正确的语法是:

insert into taxipassengers
select ...

所以,有is no values() clause

答案 1 :(得分:0)

试试这个,对于别名我们不需要“as”

SELECT a.post_date, b.vendor_name, c.lastName, d.firstName, null as taxiGroup
    FROM (select ID,post_date from wp_posts where post_type = 'shop_order') a, 
    (SELECT order_id,vendor_name FROM wp_wcpv_commissions)  b,
    (SELECT post_id,meta_value as lastName  FROM wp_postmeta  where  meta_key ='_billing_last_name') c, 
    (SELECT post_id,meta_value as firstName FROM wp_postmeta WHERE  meta_key ='_billing_first_name') d
    WHERE a.ID = b.order_id and b.order_id=c.post_id and c.post_id = d.post_id;