gpt4 book ai didi

mysql - 尝试获取具有相应名称的最小值和最大值

转载 作者:行者123 更新时间:2023-11-29 13:57:32 25 4
gpt4 key购买 nike

尝试在mysql中写入,以显示具有最大和最短时间的约会的Appointment_duration、mechanic_Firstname、mechanic_lastname、customer_firstname和customer_lastname,但我的代码给出了错误的结果。我只想要最大和最短持续时间以及相应的仅名称。所以 2 行

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

最佳答案

问题是,两个表 customermechanic 之间没有关系,您必须分别获取每个表的最大和最短持续时间并使用 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

如果所有机械师的姓名和客户的姓名都列在 firstnamelastname 两列中,您可以添加一个标记来标记来自客户的机械师。

关于mysql - 尝试获取具有相应名称的最小值和最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15608850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com