合并来自不同表的 2 SQL 个查询
combining 2 SQL queries from different tables
我有一个包含医生和过去预约的预约数据库。
当用户想预约医生时,他会选择一个住院医师,之后他将根据该住院医师显示医生。现在是棘手的部分:
医生将按照过去预约的顺序显示,这意味着如果我过去去看了两位心脏病专家,我预约的最后一位将首先显示,在他之后是另一位,在他们之后,我从未预约过所有其他心脏病专家。
为此,我需要查找我过去与心脏病专家的约会并按日期降序排列,然后去找所有医生并将其他医生添加到列表底部。
我正在尝试这个查询:
(
select distinct pastappointments.doctorID from pastappointments where
pastappointments.insuredID = 1 and pastappointments.residency='cardio'
order by pastappointments.appTime desc
)
union (
select employees.employeeID from employees where
employees.Residency='cardio' and employees.employeeID not in (
select distinct pastappointments.doctorID from pastappointments where
pastappointments.insuredID = 1 and pastappointments.residency='cardio'
))
我遇到的问题主要是 运行 union 命令上面的第一个查询,没有括号我得到了正确的顺序,一旦括号查询结果变为错误的.
我正在使用 MySQL。
我希望使用一个查询而不是两个不同的查询来执行此操作,但我似乎无法找到一种方法来做到这一点而不丢失我需要的降序。
如果您想要特定顺序的结果,则需要在最外层查询中指定 order by
。
在MySQL中,这是在之后的union
。我还强烈建议使用 union all
而不是 union
,除非您明确希望承担删除重复项的开销:
(select pa.doctorID, max(pa.appTime) as last_apptime
from pastappointments pa
where pa.insuredID = 1 and pa.residency = 'cardio'
group by pa.doctorID
) union all
(select e.employeeID, NULL as last_apptime
from employees e
where e.Residency = 'cardio' and
e.employeeID not in (select pa.doctorID
from pastappointments pa
where pa.insuredID = 1 and
pa.residency = 'cardio'
)
)
order by (last_apptime is not null) desc, last_apptime desc;
这returns两栏。如果你想要一列,只需使用子查询:
select t.doctorID
from (<above query without orderby) t
order by (last_apptime is not null) desc, last_apptime desc;
我还应该注意第一个查询中 group by
的使用。这是获得正确的最后预约时间所必需的。
而且,如果我假设所有医生都是雇员,这可以大大简化:
select e.employeeID
from employees e left join
(select pa.doctorID, max(apptime) as last_apptime
from pastappointments pa
where pa.insuredID = 1 and pa.residency = 'cardio'
group by pa.doctorId
) pa
on e.EmployeeId = pa.DoctorId
where e.Residency = 'cardio' -- or pa.doctorId is not null
order by (last_apptime is not null) desc, last_apptime;
这也假定 e.Residency
与 pa.residency
相同。如果不是这种情况,则需要 or
条件。
这样写查询比较合理(假设所有医生都是员工)。
您必须用外部查询包围这两个查询,并对其应用 order by。
SELECT * FROM (
SELECT DISCTINCT pastappointments.doctorID
FROM pastappointments
WHERE pastappointments.insuredID = 1
AND pastappointments.residency='cardio')
UNION
(SELECT employees.employeeID
FROM employees
WHERE employees.Residency='cardio'
AND employees.employeeID NOT IN
(SELECT DISTINCT pastappointments.doctorID
FROM pastappointments
WHERE pastappointments.insuredID = 1
AND pastappointments.residency='cardio'))
)
ORDER BY pastappointments.appTime DESC
您可以使用 LEFT JOIN 并将缺少的约会时间替换为最后订购的值。
select employees.employeeID
from employees
left join pastappointments
on pastappointments.insuredID = 1
and pastappointments.residency='cardio'
and pastappointments.doctorID = employees.employeeID
group by employees.employeeID
where employees.Residency='cardio'
order by coalesce(max(pastappointments.appTime), cast(0 as datetime)) desc
我有一个包含医生和过去预约的预约数据库。 当用户想预约医生时,他会选择一个住院医师,之后他将根据该住院医师显示医生。现在是棘手的部分:
医生将按照过去预约的顺序显示,这意味着如果我过去去看了两位心脏病专家,我预约的最后一位将首先显示,在他之后是另一位,在他们之后,我从未预约过所有其他心脏病专家。
为此,我需要查找我过去与心脏病专家的约会并按日期降序排列,然后去找所有医生并将其他医生添加到列表底部。
我正在尝试这个查询:
(
select distinct pastappointments.doctorID from pastappointments where
pastappointments.insuredID = 1 and pastappointments.residency='cardio'
order by pastappointments.appTime desc
)
union (
select employees.employeeID from employees where
employees.Residency='cardio' and employees.employeeID not in (
select distinct pastappointments.doctorID from pastappointments where
pastappointments.insuredID = 1 and pastappointments.residency='cardio'
))
我遇到的问题主要是 运行 union 命令上面的第一个查询,没有括号我得到了正确的顺序,一旦括号查询结果变为错误的.
我正在使用 MySQL。
我希望使用一个查询而不是两个不同的查询来执行此操作,但我似乎无法找到一种方法来做到这一点而不丢失我需要的降序。
如果您想要特定顺序的结果,则需要在最外层查询中指定 order by
。
在MySQL中,这是在之后的union
。我还强烈建议使用 union all
而不是 union
,除非您明确希望承担删除重复项的开销:
(select pa.doctorID, max(pa.appTime) as last_apptime
from pastappointments pa
where pa.insuredID = 1 and pa.residency = 'cardio'
group by pa.doctorID
) union all
(select e.employeeID, NULL as last_apptime
from employees e
where e.Residency = 'cardio' and
e.employeeID not in (select pa.doctorID
from pastappointments pa
where pa.insuredID = 1 and
pa.residency = 'cardio'
)
)
order by (last_apptime is not null) desc, last_apptime desc;
这returns两栏。如果你想要一列,只需使用子查询:
select t.doctorID
from (<above query without orderby) t
order by (last_apptime is not null) desc, last_apptime desc;
我还应该注意第一个查询中 group by
的使用。这是获得正确的最后预约时间所必需的。
而且,如果我假设所有医生都是雇员,这可以大大简化:
select e.employeeID
from employees e left join
(select pa.doctorID, max(apptime) as last_apptime
from pastappointments pa
where pa.insuredID = 1 and pa.residency = 'cardio'
group by pa.doctorId
) pa
on e.EmployeeId = pa.DoctorId
where e.Residency = 'cardio' -- or pa.doctorId is not null
order by (last_apptime is not null) desc, last_apptime;
这也假定 e.Residency
与 pa.residency
相同。如果不是这种情况,则需要 or
条件。
这样写查询比较合理(假设所有医生都是员工)。
您必须用外部查询包围这两个查询,并对其应用 order by。
SELECT * FROM (
SELECT DISCTINCT pastappointments.doctorID
FROM pastappointments
WHERE pastappointments.insuredID = 1
AND pastappointments.residency='cardio')
UNION
(SELECT employees.employeeID
FROM employees
WHERE employees.Residency='cardio'
AND employees.employeeID NOT IN
(SELECT DISTINCT pastappointments.doctorID
FROM pastappointments
WHERE pastappointments.insuredID = 1
AND pastappointments.residency='cardio'))
)
ORDER BY pastappointments.appTime DESC
您可以使用 LEFT JOIN 并将缺少的约会时间替换为最后订购的值。
select employees.employeeID
from employees
left join pastappointments
on pastappointments.insuredID = 1
and pastappointments.residency='cardio'
and pastappointments.doctorID = employees.employeeID
group by employees.employeeID
where employees.Residency='cardio'
order by coalesce(max(pastappointments.appTime), cast(0 as datetime)) desc