使用INNER JOIN的SQL查询将无法正确排序。

时间:2011-07-29 21:43:34

标签: mysql inner-join

我创建了以下SQL查询以从数据库导出订单数据。

SELECT orders.orders_id as uuid,
        orders.customers_id as order__customer_id,
        orders.customers_name as order__customer_name,
        orders.customers_company as order__customer_company,
        orders.customers_street_address as order__customer_street_address,
        orders.customers_suburb as order__customer_suburb,
        orders.customers_city as order__customer_city,
        orders.customers_postcode as order__customer_postcode,
        orders.customers_state as order__customer_state,
        orders.customers_country as order__customer_country,
        orders.customers_telephone as order__customer_telephone,
        orders.customers_email_address as order__customer_email_address,
        orders.delivery_name as order__delivery_name,
        orders.delivery_company as order__delivery_company,
        orders.delivery_street_address as order__delivery_street_address,
        orders.delivery_suburb as order__delivery_suburb,
        orders.delivery_city as order__delivery_city,
        orders.delivery_postcode as order__delivery_postcode,
        orders.delivery_state as order__delivery_state,
        orders.delivery_country as order__delivery_country,
        orders.billing_name as order__billing_name,
        orders.billing_company as order__billing_company,
        orders.billing_street_address as order__billing_street_address,
        orders.billing_suburb as order__billing_suburb,
        orders.billing_city as order__billing_city,
        orders.billing_postcode as order__billing_postcode,
        orders.billing_state as order__billing_state,
        orders.billing_country as order__billing_country,
        orders.payment_method as order__payment_method,
        orders.last_modified as order__last_modified,
        orders.date_purchased as order__date_purchased,
        orders.orders_status as order__order_status,
        orders_status.orders_status_name as order__order_status_name,
        orders.orders_date_finished as order__date_finished,
        orders.currency as order__currency,
        orders.currency_value as order__currency_value,
        ot1.orders_id,
        value AS ot_subtotal,
        ot2.ot_shipping,
        ot3.ot_total
        FROM orders, orders_status, orders_total as ot1 
        INNER JOIN ( SELECT orders_id, value AS ot_shipping FROM orders_total WHERE class='ot_shipping' ) AS ot2 ON ot1.orders_id=ot2.orders_id AND ot1.class='ot_subtotal' 
        INNER JOIN ( SELECT orders_id, value AS ot_total FROM orders_total WHERE class='ot_total' ) AS ot3 ON ot1.orders_id=ot3.orders_id AND ot1.class='ot_subtotal'
        WHERE ot1.orders_id = ot2.orders_id and orders.orders_status = orders_status.orders_status_id;

它很棒,除了它为每条记录创建4行。目前有4个订单,所以有16行(17个包括列名),当时应该只有4个(5个包括列名)。

谢谢!

当它只应该是4行(包括列名称的5行),因为只有4个订单。

非常感谢!

2 个答案:

答案 0 :(得分:1)

您似乎错过了将JOIN链接到orders的任何orders_total。如果您使用显式JOIN语法重写而不是混合使用新旧样式,这将更加清晰。

FROM   orders
       INNER JOIN orders_status ON orders.orders_status = orders_status.orders_status_id
       INNER JOIN orders_total as ot1 /*<--Missing Condition here
                                          ON orders.orders_id = ot1.orders_id */
       INNER JOIN (SELECT orders_id,
                          value AS ot_shipping
                   FROM   orders_total
                   WHERE  class = 'ot_shipping') AS ot2
         ON ot1.orders_id = ot2.orders_id
       INNER JOIN (SELECT orders_id,
                          value AS ot_total
                   FROM   orders_total
                   WHERE  class = 'ot_total') AS ot3
         ON ot1.orders_id = ot3.orders_id

在任何情况下,您都不需要为此加入同一个表3次。您可以使用以下内容。

SELECT orders.orders_id,
       MAX(CASE
             WHEN orders_total.class = 'ot_subtotal' THEN value
           END) AS ot_subtotal,
       MAX(CASE
             WHEN orders_total.class = 'ot_shipping' THEN value
           END) AS ot_shipping,
       MAX(CASE
             WHEN orders_total.class = 'ot_total' THEN value
           END) AS ot_total
FROM   orders
       INNER JOIN orders_status
         ON orders.orders_status = orders_status.orders_status_id
       INNER JOIN orders_total
         ON orders_total.orders_id = orders.orders_id
WHERE  orders_total.class in ( 'ot_subtotal', 'ot_shipping', 'ot_total' )
GROUP  BY orders.orders_id  

答案 1 :(得分:0)

尝试在查询中使用SELECT DISTINCT。

SELECT DISTINCT orders.orders_id as uuid...
FROM orders, orders_status, orders_total as ot1 
INNER JOIN ( SELECT orders_id, value AS ot_shipping FROM orders_total WHERE class='ot_shipping' ) AS ot2 ON ot1.orders_id=ot2.orders_id AND ot1.class='ot_subtotal' 
INNER JOIN ( SELECT orders_id, value AS ot_total FROM orders_total WHERE class='ot_total' ) AS ot3 ON ot1.orders_id=ot3.orders_id AND ot1.class='ot_subtotal'
WHERE ot1.orders_id = ot2.orders_id and orders.orders_status = orders_status.orders_status_id;

编辑:

我使用所有表上的INNER JOIN而不是','将FROM部分更新为下面的部分。我不知道你的表结构,所以我猜你的连接字段。

FROM orders
INNER JOIN orders_status
    ON orders.orders_status = orders_status.orders_status_id
INNER JOIN orders_total as ot1 
    ON orders.orders_id = ot1.orders_id
INNER JOIN ( SELECT orders_id, value AS ot_shipping FROM orders_total WHERE class='ot_shipping' ) AS ot2 
    ON ot1.orders_id=ot2.orders_id 
INNER JOIN ( SELECT orders_id, value AS ot_total FROM orders_total WHERE class='ot_total' ) AS ot3 
    ON ot1.orders_id=ot3.orders_id
WHERE ot1.class='ot_subtotal'