我有三张桌子:
*orders:
-id (PK)
-iduser (FK)
-date
*detail_orders:
-id(PK)
-or_id (FK of id on orders)
-prod_id (FK of id on products)
-price
-quantity
*products:
-id (PK)
-description
有订单的表格,订单的另一个明细(产品,价格,数量)和另一个要检索描述的产品
我想从特定的iduser(从php检索)使用mysql来获取它:
order.id | order.date:
products.description | orders.quantity | orders.price
products.description | orders.quantity | orders.price
products.description | orders.quantity | orders.price
... (etc while there are products on this order)
查询应该如何?我有经验只从一个sql表中检索数据,并且没有来自多个数据库的连接数据。
答案 0 :(得分:2)
您需要JOIN
表格,如下所示:
SELECT products.description, orders.quantity, orders.price
FROM detail_orders
INNER JOIN products ON (products.prod_id = detail_orders.prod_id)
INNER JOIN orders ON (orders.id = detail_orders.or_id)
如果您想查询特定用户:
SELECT products.description, orders.quantity, orders.price
FROM detail_orders
INNER JOIN products ON (products.prod_id = detail_orders.prod_id)
INNER JOIN orders ON (orders.id = detail_orders.or_id)
WHERE orders.iduser = someUser