2011-07-29 8 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つのオーダーがあるので、列名を含めて4つ(列名を含む5つ)にする必要がある場合は、16の行(列名を含む17)があります。

ありがとうございます!

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; 

EDIT:

私はあなたの部分からINNERを使用して以下に更新が「」の代わりに、すべてのテーブルに結合します。私はあなたのテーブル構造を知らないので、あなたの参加フィールドを推測しています。

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_totalordersをリンクする任意のJOINが欠けているように見えます。新しいスタイルと古いスタイルを混ぜるのではなく、明示的に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回参加する必要はありません。あなたは以下のようなものを使うことができます。

+0

ot_shipping、ot_totalなどの値を定義せずにこれを行うことができますか? – PHRoG

関連する問題