MySQL的全新内容。我正在寻找从数据库获取报告的最佳解决方案。
以下是详细信息:
Customer_order - order_id
,customer_id
,order_name
,order_price
,order_store_id
Customer_details - customer_id
,customer_name
,customer_emp_id
Customer_order_items - order_id
,customer_id
,product_name
我正在尝试从 Customer_order_items , customer_order , customer_details 获取所有详细信息,并通过向{提供特定数据显示结果{1}}
所以我尝试了以下内容:
order_store_id
我得到了结果,但奇怪的是我得到了所有的双倍,三倍的值。
如果我的查询正确,我应该怎么做?
让我知道!
答案 0 :(得分:0)
您应该将LEFT添加到JOIN条件
LEFT JOIN强制选择LEFT表的所有行(JOIN条件)和RIGHT表中的相应行。
SELECT d.customer_name, d.company_id, p.store_id, p.order_id, a.product_id, a.service_id
FROM customer_master d
LEFT JOIN customer_orders p ON d.customer_id = p.customer_id
LEFT JOIN customer_order_items a ON a.customer_id = d.customer_id
WHERE p.order_store_id = "000020"
答案 1 :(得分:0)
我会说你的查询错了,你的数据库设计错了(为什么你会在订单商品级别有客户ID?)假设订单和订单商品之间的订单号有链接我想我可以像这样复制你的问题
ariaDB [sandbox]> select * from customers;
+------+------+
| id | name |
+------+------+
| 1 | John |
| 2 | Jane |
+------+------+
2 rows in set (0.00 sec)
MariaDB [sandbox]> select * from orders;
+-------------+---------+------------+------------+
| ordernumber | cust_id | orderdate | shipdate |
+-------------+---------+------------+------------+
| 1 | 1 | 2016-01-01 | 2016-02-01 |
| 2 | 1 | 2016-02-01 | NULL |
+-------------+---------+------------+------------+
2 rows in set (0.00 sec)
MariaDB [sandbox]> select * from order_details;
+------+-------------+-----------------+-------------+---------+
| id | ordernumber | quantityordered | quotedprice | cust_id |
+------+-------------+-----------------+-------------+---------+
| 1 | 1 | 10 | 10 | 1 |
| 2 | 1 | 20 | 20 | 1 |
+------+-------------+-----------------+-------------+---------+
2 rows in set (0.00 sec)
根据这些数据,我希望在我的结果中使用您的查询得到2行(根据我的数据进行修改)
MariaDB [sandbox]> select c.id,c.name,o.ordernumber,o.orderdate,od.quantityordered
-> from customers c
-> join orders o on o.cust_id = c.id
-> join order_details od on od.cust_id = c.id
-> order by ordernumber;
但实际上我得了4行
+------+------+-------------+------------+-----------------+
| id | name | ordernumber | orderdate | quantityordered |
+------+------+-------------+------------+-----------------+
| 1 | John | 1 | 2016-01-01 | 20 |
| 1 | John | 1 | 2016-01-01 | 10 |
| 1 | John | 2 | 2016-02-01 | 10 |
| 1 | John | 2 | 2016-02-01 | 20 |
+------+------+-------------+------------+-----------------+
4 rows in set (0.00 sec)
因为在客户ID上加入了order_details。 更合理的方法是将连接级联到order_details连接到ordernumber上的订单(如果订单没有order_details,则使用左连接)
ariaDB [sandbox]> select c.id,c.name,o.ordernumber,o.orderdate,od.quantityordered
-> from customers c
-> join orders o on o.cust_id = c.id
-> left join order_details od on od.ordernumber = o.ordernumber
-> order by ordernumber;
+------+------+-------------+------------+-----------------+
| id | name | ordernumber | orderdate | quantityordered |
+------+------+-------------+------------+-----------------+
| 1 | John | 1 | 2016-01-01 | 20 |
| 1 | John | 1 | 2016-01-01 | 10 |
| 1 | John | 2 | 2016-02-01 | NULL |
+------+------+-------------+------------+-----------------+
这给了我期望的3行。