Mysql 尝试使用相应的名称获取最小值和最大值
尝试在mysql中编写,显示约会的最长时间、最短时间、最长时间、最长时间、最长时间、最长时间、最长时间、最长时间、最长时间和最长时间,但我的代码给出了错误的结果。。我只想要最大和最小持续时间以及相应的名称。so 2排Mysql 尝试使用相应的名称获取最小值和最大值,mysql,Mysql,尝试在mysql中编写,显示约会的最长时间、最短时间、最长时间、最长时间、最长时间、最长时间、最长时间、最长时间、最长时间和最长时间,但我的代码给出了错误的结果。。我只想要最大和最小持续时间以及相应的名称。so 2排 SELECT Min(Appointment.Appointment_duration) AS MINOfAppointment_duration, Max(Appointment.Appointment_duration) AS MAXOfAppointment_durati
SELECT
Min(Appointment.Appointment_duration) AS MINOfAppointment_duration,
Max(Appointment.Appointment_duration) AS MAXOfAppointment_duration,
mechanic_Firstname, mechanic_lastname, customer_firstname, customer_lastname
FROM Appointment, mechanic, Customer
WHERE (mechanic.mechanic_ID=Appointment.mechanic_ID) AND (customer.customer_ID=Appointment.customer_ID);
预约表中的记录
Appointment_ID Appointment_DATE Appointment_Duration Mechanic_ID Customer_ID
12 08/01/2007 0:35:00 1 5684
13 01/01/2009 2:15:36 6 2534
14 06/12/2010 0:05:29 7 7423
您可以使用
Union all
函数查找最大值和最小值
SELECT
Min(Appointment.Appointment_duration) AS Appointment_duration, 'Min' as status,
mechanic_Firstname, mechanic_lastname, customer_firstname, customer_lastname
FROM Appointment, mechanic, Customer
WHERE (mechanic.mechanic_ID=Appointment.mechanic_ID)
AND (customer.customer_ID=Appointment.customer_ID);
Union all
SELECT
Max(Appointment.Appointment_duration) AS Appointment_duration,'Max' as status,
mechanic_Firstname, mechanic_lastname, customer_firstname, customer_lastname
FROM Appointment, mechanic, Customer
WHERE (mechanic.mechanic_ID=Appointment.mechanic_ID)
AND (customer.customer_ID=Appointment.customer_ID);
问题是,两个表
customer
和mechanical
之间没有关系,您必须分别获得每个表的最大和最小持续时间,并使用UNION ALL
将两个结果集合并为一个。比如:
SELECT
m.mechanic_Firstname AS FirstName,
m.mechanic_lastname AS LastName,
IFNULL(Min(a.Appointment_duration), 0) AS MINOfAppointment_duration,
IFNULL(Max(a.Appointment_duration), 0) AS MAXOfAppointment_duration
FROM mechanic AS m
LEFT JOIN Appointment AS a ON a.mechanic_ID = m.mechanic_ID
GROUP BY m.mechanic_Firstname,
m.mechanic_lastname
UNION ALL
SELECT
c.customer_firstname,
c.customer_lastname,
IFNULL(Min(a.Appointment_duration), 0),
IFNULL(Max(a.Appointment_duration), 0)
FROM customer AS c
LEFT JOIN Appointment AS a ON a.mechanic_ID = c.customer_ID
GROUP BY c.customer_firstname,
c.customer_lastname;
这将只提供四列:
FirstName | LastName | MINOfAppointment_duration | MAXOfAppointment_duration
如果所有机械师的姓名和客户的姓名都列在两列中
firstname
和lastname
,您可以添加一个标志来标记来自客户的机械师。您可以提供示例记录吗?包括预约表,