3 に答える
2
subquery
1 つにつき 1 つの製品のみを取得するエクストラを追加します。order_id
Select
orders.id,
orders.order_price,
orders.purchase_date,
customers.email,
product_orders.qty,
products.name
from
orders
INNER JOIN product_orders on orders.id=product_orders.orders_id
INNER JOIN
(
SELECT orders_id, MIN(Product_ID) prodID
FROM product_orders
GROUP BY orders_id
) c ON product_orders.orders_ID = c.orders_id AND
product_orders.Product_ID = c.prodID
INNER JOIN products on product_orders.product_id=products.id
INNER JOIN customers on orders.customer_id =customers.id
SQLFiddle デモ
于 2012-10-16T09:03:46.667 に答える
1
3つの新しいテーブルが必要です。
Bundles (ID, Name)
BundleProducts (BundleID, ProductID)
OrderLines (ID, OrderID, ProductID, BundleID, Qty)
バンドルに含まれているとしましょう:
ID Name
1 IPad & Android
そしてBundleProductsは含まれています
BundleID ProductID
1 2
1 3
次に、顧客が商品を注文に追加するときに、ProductIDをOrderLinesテーブルに挿入します。バンドルを追加する場合は、BundleIDに挿入します。注文が完了すると、Product_Ordersテーブルに、Product_IDが入力されているOrderLinesテーブルの各製品と、ProductLines.BundleIDが入力されている場合はBundleProductsテーブルの各ProductIDが入力されます。
または、バンドルを製品自体にすることもできます。これは、Bundlesテーブルは必要ないが、BundleProductsテーブルは必要であることを意味します。また、OrderLinesのBundleID列は必要ありません。そのため、「Ipad&AndroidBundle」はProductsテーブルの新製品になります。
注文が完了すると、次のようになります。
INSERT INTO
Product_Orders (Orders_id, Product_ID, Qty)
SELECT
Orders.ID,
COALESCE(BundleProducts.ProductID, OrderLines.ProductID),
OrderLines.Qty
FROM
Orders
INNER JOIN OrderLines ON Orders.ID = OrderLines.Orders_ID
LEFT JOIN BundleProducts ON OrderLines.ProductID = BundleLines.BundleID
于 2012-10-16T09:44:29.160 に答える
0
Select orders.id, orders.order_price, orders.purchase_date, customers.email, product_orders.qty, products.name
from orders
INNER JOIN product_orders on orders.id=product_orders.orders_id
INNER JOIN products on product_orders.product_id=products._id
INNER JOIN customers on orders.customer_id =customers.id
GROUP BY orders.id
GROUP BY orders.id を追加します
于 2012-10-16T08:57:42.200 に答える