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

时间:2017-01-07 14:29:24

标签: mysql sql sql-order-by union

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

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

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

我正在尝试此查询:

...
`ScriptingziLua_dbgI'
/tmp/ghc14828_0/ghc_11.o:ghc_8.c:(.text+0x100): first defined here
/tmp/ghc14828_0/ghc_11.o: In function `ScriptingziLua_dbgR':
ghc_8.c:(.text+0x190): multiple definition of `ScriptingziLua_dbgR'
/tmp/ghc14828_0/ghc_11.o:ghc_8.c:(.text+0x190): first defined here
/tmp/ghc14828_0/ghc_11.o: In function `ScriptingziLua_dbh0':
ghc_8.c:(.text+0x260): multiple definition of `ScriptingziLua_dbh0'
/tmp/ghc14828_0/ghc_11.o:ghc_8.c:(.text+0x260): first defined here
collect2: error: ld returned 1 exit status
stack build --only-dependencies returned exit code 1

我遇到的问题主要是在union命令上面运行第一个查询时,没有括号我得到了正确的顺序,一旦括号,查询结果变为错误的。< / p>

我正在使用MySQL。

我希望使用一个查询而不是两个不同的查询,但我似乎无法找到一种方法,而不会丢失我需要的降序。

3 个答案:

答案 0 :(得分:2)

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

在MySQL中,这是之后的{em> 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;

返回两列。如果您想要一个列,只需使用子查询:

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.Residencypa.residency相同。如果不是这种情况,则需要or条件。

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

答案 1 :(得分:0)

您必须通过外部查询围绕这两个查询,并将顺序应用于该查询。

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

答案 2 :(得分:0)

您可以使用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