从多个查询生成访问报告
Generate Access report from multiple queries
我已经浏览了一些关于这个主题的答案,但他们都建议使用 UNION 查询,我很确定这不是我想要提取的 tables 的方法来自的数据根本不同。我会尽力解释。
我有三个table:
- Customers
- Appointments
- Purchases
我希望用户能够 select 单个客户并收到包含以下信息的报告:
- All the customer information (Name, email, address etc.)
- All the appointments booked by the customer
- All the purchases made by the customer
当客户在数据库中没有约会或购买时会出现问题,因为我目前使用的单个查询执行以下操作:
SELECT (info needed)
FROM (Customer INNER JOIN Purchase ON Customer.ID = Purchase.customerID)
INNER JOIN Appointment ON Customer.ID = Appointment.customerID;
如果客户在 Purchase 或 Appointment 中没有他们的 customerID table 结果查询将 return 为空 table 因为 JOIN 将无法看到任何table 之间的共享数据。
我不确定最好的行动方案是什么,我希望这里有人可以阐明在这种情况下最好的做法是什么,在此先感谢。
建议编辑以添加一些示例数据和所需结果。
Customers
ID Name email address
1 Bob bob@gmail.com 70 bowman st.
2 Tom tom@gmail.com 44 shirley av.
3 Mary mary@gmail.com 4 goldfield rd.
Purchases
customerID itemID price
1 23 100
1 24 150
3 34 85
Appointments
customerID date
1 2/10
2 3/10
3 3/10
现在,如果我使用 customerID = 2 进行查询,我需要以下信息
Customer Info
name email address
Tom tom@gmail.com 44 shirley av.
Purchases
empty
Appointments
date
3/10
使用左联接而不是内联接。这样即使他没有预约或购买,您也可以得到客户的详细信息
SELECT (info needed)
FROM Customer Left
JOIN Purchase
ON Customer.ID = Purchase.customerID
Left JOIN Appointment
ON Customer.ID = Appointment.customerID;
我已经浏览了一些关于这个主题的答案,但他们都建议使用 UNION 查询,我很确定这不是我想要提取的 tables 的方法来自的数据根本不同。我会尽力解释。
我有三个table:
- Customers
- Appointments
- Purchases
我希望用户能够 select 单个客户并收到包含以下信息的报告:
- All the customer information (Name, email, address etc.)
- All the appointments booked by the customer
- All the purchases made by the customer
当客户在数据库中没有约会或购买时会出现问题,因为我目前使用的单个查询执行以下操作:
SELECT (info needed)
FROM (Customer INNER JOIN Purchase ON Customer.ID = Purchase.customerID)
INNER JOIN Appointment ON Customer.ID = Appointment.customerID;
如果客户在 Purchase 或 Appointment 中没有他们的 customerID table 结果查询将 return 为空 table 因为 JOIN 将无法看到任何table 之间的共享数据。
我不确定最好的行动方案是什么,我希望这里有人可以阐明在这种情况下最好的做法是什么,在此先感谢。
建议编辑以添加一些示例数据和所需结果。
Customers
ID Name email address
1 Bob bob@gmail.com 70 bowman st.
2 Tom tom@gmail.com 44 shirley av.
3 Mary mary@gmail.com 4 goldfield rd.
Purchases
customerID itemID price
1 23 100
1 24 150
3 34 85
Appointments
customerID date
1 2/10
2 3/10
3 3/10
现在,如果我使用 customerID = 2 进行查询,我需要以下信息
Customer Info
name email address
Tom tom@gmail.com 44 shirley av.
Purchases
empty
Appointments
date
3/10
使用左联接而不是内联接。这样即使他没有预约或购买,您也可以得到客户的详细信息
SELECT (info needed)
FROM Customer Left
JOIN Purchase
ON Customer.ID = Purchase.customerID
Left JOIN Appointment
ON Customer.ID = Appointment.customerID;