2011-07-29 83 views
1

我已創建以下SQL查詢以從數據庫中導出訂單數據。使用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個時(包括列名)。

謝謝!

當它只應該是4行(5包括列名稱),因爲只有4個訂單。

非常感謝!

回答

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; 

編輯:

我更新了與部分的下方內部採用的所有表,而不是JOIN的「」。我不知道你的表結構,所以我猜測你的連接字段。

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' 
+0

給了我與上面相同的問題。 16行,而不是4. – PHRoG

+0

我在上面添加了一條新的FROM語句。 – Taryn

+0

WOOHOO !! :D那樣做......非常感謝你! :) – PHRoG

1

你似乎缺少能夠聯繫到orders任何orders_totalJOIN。如果用明確的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 
+0

你可以做到這一點,而無需定義值ot_shipping,ot_total等等,仍然有它通過訂單ID排序全部? – PHRoG