如何合并这些SQL查询

时间:2012-12-22 16:02:26

标签: mysql

我需要合并此查询

SELECT 
*,
(SELECT 
        CONCAT(c.firstname, ' ', c.lastname)
    FROM
        wwwpser_customer c
    WHERE
        c.customer_id = o.customer_id) AS customer
FROM
wwwpser_order o
WHERE
o.order_id = '20'

SELECT 
orders . *, wwwpser_comuna.provincia_id AS payment_provincia_id
FROM
wwwpser_order orders
    LEFT JOIN
wwwpser_comuna ON (orders.payment_city = wwwpser_comuna.comuna_id) 

SQL语法对我来说是新的,所以我需要一点帮助,thx

2 个答案:

答案 0 :(得分:1)

SELECT CONCAT(c.firstname, ' ', c.lastname)
,      wc.provincia_id 
,      o.*
FROM   wwwpser_order o
LEFT JOIN   
       wwwpser_customer c
ON     c.customer_id = o.customer_id
LEFT JOIN
       wwwpser_comuna wc
ON     wc.comuna_id = o.payment_city
WHERE  o.order_id = 20

答案 1 :(得分:0)

看起来这应该是你的解决方案。

SELECT 
o.*,                                                -- modified (qualified the
(SELECT                                             -- asterisk)
        CONCAT(c.firstname, ' ', c.lastname)
    FROM
        wwwpser_customer c
    WHERE
        c.customer_id = o.customer_id) AS customer, 
c.provincia_id AS payment_provincia_id              -- added from second query
FROM
wwwpser_order o
    LEFT JOIN                                       -- added from second query
wwwpser_comuna c ON (o.payment_city = c.comuna_id)  -- added from second query
WHERE
o.order_id = '20'

但我强烈建议您学习一般的SQL语法。 MySQL tutorials

中有一些有用的示例