我的问题是我目前正在编写的客户服务数据库。我对 sql 查询了解不多,而且我真的很难正确查询。
我有两张桌子:
**Customers:**
idcustomer
company_name
tel_number
address
postcode
**customer_machines**
id_customer_machine
id_customer
date_last_service
date_service_due
我想选择显示在给定日期集之间哪些机器到期提供服务的结果(例如机器在第 9 个月到期)。
SELECT customer_machines.* from customer_machines WHERE (customer_machines.date_next_service BETWEEN '2013-09-01' AND '2013-10-01') ORDER BY date_next_service ASC;
这很好,但是有些客户拥有不止一台机器,例如
+---------------------+-------------+-------------------+-------------------+
| id_customer_machine | id_customer | date_last_service | date_next_service |
+---------------------+-------------+-------------------+-------------------+
| 1 | 1 | 2012-09-02 | 2013-09-02 |
| 2 | 2 | 2012-09-14 | 2013-09-14 |
| 3 | 3 | 2012-09-30 | 2013-09-30 |
| 5 | 3 | 2012-09-30 | 2013-09-30 |
+---------------------+-------------+-------------------+-------------------+
我需要将客户拥有的机器分组,加入客户的详细信息并输出到浏览器中的表格,例如:
Customer 1 address of customer 1 tel of customer 1 postcode of customer 1
machine-id-1 date-last-service date-next-service
------------------------------------------------------------------------------------
customer 2 address of customer 2 tel of customer 2 postcode of customer 2
machine-id-2 date-last-service date-next-service
-------------------------------------------------------------------------------------
customer 3 address of customer 3 tel of customer 3 postcode of customer 3
machine-id-3 date-last-service date-next-service
machine-id-5 date-last-service date-next-service
我可以像这样通过单个查询将结果排列在一起吗?到目前为止,我只尝试过在 php 中嵌套查询,但没有成功。
如果你能指出我正确的方向那就太好了
谢谢