2014-03-29 9 views
1

我有兩個查詢單獨返回正確的數據,但是當我試圖加入它們時,返回的數據是錯誤的。 查詢一個是:加入兩個正在運行的sql查詢會得到錯誤的結果 - 我在哪裏出錯

SELECT op.orders_id, op.products_id, op.products_name, op.final_price, op.products_quantity 
FROM orders_products op 
WHERE op.orders_id = 21535 

查詢二是:

SELECT opa.products_options_values_id, opa.products_prid 
FROM orders_products_attributes opa 
WHERE opa.orders_id = 21535 

我試圖合併查詢是:

SELECT op.orders_id, op.products_id, op.products_model, op. products_name, op.final_price, op.products_quantity, opa.products_options_values_id, opa.products_prid 
FROM orders_products op 
JOIN orders_products_attributes opa 
ON op.orders_id = opa.orders_id 
WHERE op.orders_id = 21535 

小提琴是在這裏:http://www.sqlfiddle.com/#!8/ef9be/5

我不想法如何解決這個問題。我在opa.products_options_values_id上嘗試了Group BY,但是這導致了問題,因爲數據結果中有兩個40的結果。 任何幫助表示讚賞。

回答

1

在您的join條件中,您使用了orders_id。你應該使用這樣的orders_products_id

SELECT op.orders_id, op.products_id, op.products_model, op. products_name, 
     op.final_price, op.products_quantity, opa.products_options_values_id, 
     opa.products_prid 
FROM orders_products op 
JOIN orders_products_attributes opa ON op.orders_products_id = opa.orders_products_id 
WHERE op.orders_id = 21535 
+0

非常好。這解決了它。 JOINS有點新鮮。我很驚訝,我甚至沒有給我任何東西。我每天都在這裏學習新的東西 –