Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 组合来自不同表的2个SQL查询_Mysql_Sql_Sql Order By_Union - Fatal编程技术网

Mysql 组合来自不同表的2个SQL查询

Mysql 组合来自不同表的2个SQL查询,mysql,sql,sql-order-by,union,Mysql,Sql,Sql Order By,Union,我有一个预约数据库,包含医生和过去的预约。 当用户希望预约医生时,他会选择一个住院医师,之后会根据该住院医师向他展示医生。现在是棘手的部分: 医生们将按照过去预约的顺序进行展示,这意味着如果我过去拜访过两位心脏病专家,我预约的最后一位将首先展示,在他之后是另一位,在他们之后是我从未预约过的所有其他心脏病专家 为此,我需要查找我过去与心脏病专家的预约,并按日期降序排列,然后去找所有医生,只需将其他医生添加到列表的底部 我正在尝试以下查询: ( select distinct pastappoint

我有一个预约数据库,包含医生和过去的预约。 当用户希望预约医生时,他会选择一个住院医师,之后会根据该住院医师向他展示医生。现在是棘手的部分:

医生们将按照过去预约的顺序进行展示,这意味着如果我过去拜访过两位心脏病专家,我预约的最后一位将首先展示,在他之后是另一位,在他们之后是我从未预约过的所有其他心脏病专家

为此,我需要查找我过去与心脏病专家的预约,并按日期降序排列,然后去找所有医生,只需将其他医生添加到列表的底部

我正在尝试以下查询:

(
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 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;
这将返回两列。如果需要一列,只需使用子查询:

select t.doctorID
from (<above query without orderby) t
order by (last_apptime is not null) desc, last_apptime desc;
这还假设
e.residentity
pa.residentity
相同。如果不是这样,则需要
条件


这是编写查询的一种更合理的方法(假设所有医生都是员工)。

如果希望结果按特定顺序排列,则需要在最外层的查询中指定
order by

在MySQL中,这是在
联合之后。我还强烈建议使用
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;
这将返回两列。如果需要一列,只需使用子查询:

select t.doctorID
from (<above query without orderby) t
order by (last_apptime is not null) desc, last_apptime desc;
这还假设
e.residentity
pa.residentity
相同。如果不是这样,则需要
条件


这是编写查询的一种更合理的方法(假设所有医生都是员工)。

您必须用外部查询包围这两个查询,并对其应用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

您必须用外部查询包围这两个查询,并对其应用排序依据

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

您可以使用左联接,并将缺少的约会时间替换为最后订购的值

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 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

嗨,谢谢你的回复!你的第一个建议有效,但当我尝试使用更简单的建议时,它通知我一个意外的“位置”。我还有一个小问题,我可以在使用主查询时使用子查询吗?这样我从数据库得到的只是医生的ID号?您缺少左JOIN的ON子句(
pa.doctorID=e.employeeID
)。@PaulSpiegel。谢谢。嗨,谢谢你的回复!你的第一个建议有效,但当我尝试使用更简单的建议时,它通知我一个意外的“位置”。我还有一个小问题,我可以在使用主查询时使用子查询吗?这样我从数据库得到的只是医生的ID号?您缺少左JOIN的ON子句(
pa.doctorID=e.employeeID
)。@PaulSpiegel。非常感谢。