我之前创建了一个查询,用于连接与客户关联的员工。每个客户只分配了2个客户。我的问题是在执行查询之后,对于每个客户,它将所有员工与每个客户相关联,而不是2.这是我到目前为止所拥有的:
SELECT C.customer_id,
C.l_name AS Surname,
C.f_name AS 'First Name',
C.travel_date,
T.tour_name,
Group_concat(S.f_name, S.l_name) AS Staff_Concat
FROM customers AS C
LEFT JOIN tour AS T
ON C.tour_id = T.tour_id
LEFT JOIN staff_day AS SD
ON C.tour_id = SD.tour_id
LEFT JOIN staff_day AS SD_2
ON SD_2.sd_date = C.travel_date
LEFT JOIN staff AS S
ON SD.staff_id = S.staff_id
WHERE C.travel_date >= '2014-07-08 00:00:00'
AND C.travel_date <= '2014-07-08 23:59:59'
AND C.customer_id NOT IN (SELECT O.customer_id
FROM customers AS C,
orders AS O
WHERE C.travel_date >= '2014-07-08 00:00:00'
AND C.travel_date <=
'2014-07-08 23:59:59'
AND C.customer_id = O.customer_id)
GROUP BY C.customer_id
我尝试过使用:
SD.staff_ID = S.staff_ID
在where子句中但没有太多运气。我觉得好像:
LEFT JOIN Staff AS S ON SD.staff_ID = S.staff_ID
是不必要的,但我似乎无法解释原因。这是我得到的结果:
6166 customer_Name 2014-07-08 Wildthing Staff1, Staff2, staff3...staff10
我的预期结果应该是:
6166 customer_Name 2014-07-08 Wildthing Staff1, staff2
如果这是重复,请再次道歉。我做了一些研究,但现在我已经走到了尽头。
以下是数据库架构:
Table structure for table Customers
Column Type Null Default
customer_ID int(11) No
f_Name varchar(30) Yes NULL
l_Name varchar(30) No
address varchar(100) No
suburb varchar(30) No
state varchar(30) No
country varchar(30) No
postcode varchar(30) No
email varchar(50) No
phone varchar(20) No
child_1 varchar(30) Yes NULL
child_2 varchar(30) Yes NULL
child_3 varchar(30) Yes NULL
child_4 varchar(30) Yes NULL
travel_Date datetime No
signature text No
terms tinyint(1) No
interested_video tinyint(1) Yes 0
specials tinyint(1) Yes NULL
tour_ID int(11) Yes NULL
updated timestamp No CURRENT_TIMESTAMP
Table structure for table Staff
Column Type Null Default
f_Name varchar(30) No
l_Name varchar(30) No
comm_Value int(3) No
staff_Type varchar(50) No
staff_ID int(11) No
bus_ID int(11) No
Table structure for table Staff_Day
Column Type Null Default
sd_ID int(11) No
staff_ID int(4) No
tour_ID int(4) No
sd_Date datetime No
Table structure for table Tour
Column Type Null Default
tour_Name varchar(30) No
tour_ID int(11) No
bus_ID int(11) No