我有 4 张桌子用来尝试获取客户订单:
account
- account_id
- username
product
- product_number
- product_name
customer_order
- customer_order_id
- fk_account_id (foreign key)
customer_order_details
- fk_customer_order_id (foreign key)
- fk_product_id (foreign key)
这是我的查询......这有点令人困惑,但是:
SELECT customer_order_details.fk_customer_order_id, product.product_name,
customer_order_details.colour, customer_order_details.size
FROM customer_order, customer_order_details, product, account
WHERE customer_order.customer_order_id =
customer_order_details.fk_customer_order_id
AND customer_order_details.fk_product_id = product.product_id
AND customer_order.fk_account_id = account.account_id
AND account.username = 'user1'
GROUP BY customer_order_details.fk_customer_order_id;
它应该按 customer_order_details 表中的外键 (fk_customer_order_id) 分组。我想要的结果是这样的:
1
Microfleece Vest
Jacket With Hood
2
Jacket With Hood
相反,我得到类似的东西:
1
Microfleece Vest
2
Jacket With Hood
我的 PHP 代码只是通过一个 while 循环运行并打印出 product_name 和 fk_customer_order_id。