SQL select不需要where语句

时间:2014-01-28 12:20:20

标签: sql sql-server where-clause

我想从不同的表中选择一些主要和外键链接的数据。

AppointmentID不是必需的,但当RC.CMO和RC.Appointments_RC.CMO之间没有链接时,select会给我一个null结果,导致“where”不满意。

如果没有预约cmo,我如何选择所有数据?

我认为这就是问题所在:

AND
[RC.CMO].cmo_id = [RC.Appointments_RC.CMO].cmo_id
AND
[RC.Appointments_RC.CMO].appointment_id = [RC.Appointments].appointment_id

如果此人未与约会相关联,我无法找到一些数据。

说明:

SELECT 
  [RC.CMO].ci_nummer
, [RC.CMO].raum
, [RC.CMO].anzahl_monitore as cmo_mon
, [RC.CMO].gebäude as gebaeude
, [RC.CMO].bemerkung
, [RC.Persons].kostenstelle
, [RC.Persons].vorname
, [RC.Persons].nachname
, [RC.FMO].hardware_typ
, [RC.FMO].anzahl_monitore as fmo_mon
, [RC.FMO].zubehör as zubehoer
, [RC.Appointments].appointment_id
FROM [RC.CMO]
   , [RC.Persons]
   , [RC.Persons_RC.CMO]
   , [RC.FMO]
   , [RC.Persons_RC.FMO]
   , [RC.Appointments]
   , [RC.Appointments_RC.CMO]
WHERE [RC.CMO].cmo_id = @cmo_id
AND   [RC.CMO].cmo_id = [RC.Persons_RC.CMO].cmo_id
AND   [RC.Persons_RC.CMO].person_id = [RC.Persons].person_id
AND   [RC.Persons].person_id = [RC.Persons_RC.FMO].person_id
AND   [RC.Persons_RC.FMO].fmo_id = [RC.FMO].fmo_id
AND   [RC.CMO].cmo_id = [RC.Appointments_RC.CMO].cmo_id
AND   [RC.Appointments_RC.CMO].appointment_id = [RC.Appointments].appointment_id

是否可以使存在或不存在?

2 个答案:

答案 0 :(得分:3)

如果您将查询转换为使用现代显式JOIN语法而不是旧式隐含语法,则可以使用OUTER联接,这可以完全符合您的要求:

SELECT 
  [RC.CMO].ci_nummer
, [RC.CMO].raum
, [RC.CMO].anzahl_monitore as cmo_mon
, [RC.CMO].gebäude as gebaeude
, [RC.CMO].bemerkung
, [RC.Persons].kostenstelle
, [RC.Persons].vorname
, [RC.Persons].nachname
, [RC.FMO].hardware_typ
, [RC.FMO].anzahl_monitore as fmo_mon
, [RC.FMO].zubehör as zubehoer
, [RC.Appointments].appointment_id
FROM [RC.CMO] 
JOIN [RC.Persons] ON [RC.CMO].cmo_id = [RC.Persons_RC.CMO].cmo_id
JOIN [RC.Persons_RC.CMO] ON [RC.Persons_RC.CMO].person_id = [RC.Persons].person_id
JOIN [RC.FMO] ON [RC.Persons].person_id = [RC.Persons_RC.FMO].person_id
JOIN [RC.Persons_RC.FMO] ON [RC.Persons_RC.FMO].fmo_id = [RC.FMO].fmo_id
LEFT OUTER JOIN [RC.Appointments] ON [RC.CMO].cmo_id = [RC.Appointments_RC.CMO].cmo_id
JOIN [RC.Appointments_RC.CMO] ON [RC.Appointments_RC.CMO].appointment_id = [RC.Appointments].appointment_id
WHERE [RC.CMO].cmo_id = @cmo_id

答案 1 :(得分:1)

试试这个。我删除了重复的表并优化了连接:

SELECT [RC.CMO].ci_nummer
     , [RC.CMO].raum
     , [RC.CMO].anzahl_monitore as cmo_mon
     , [RC.CMO].gebäude as gebaeude
     , [RC.CMO].bemerkung
     , [RC.Persons].kostenstelle
     , [RC.Persons].vorname
     , [RC.Persons].nachname
     , [RC.FMO].hardware_typ
     , [RC.FMO].anzahl_monitore as fmo_mon
     , [RC.FMO].zubehör as zubehoer
     , [RC.Appointments].appointment_id
  FROM [RC.CMO]
 inner join
       [RC.Persons_RC.CMO]
    on [RC.Persons_RC.CMO].cmo_id = [RC.CMO].cmo_id
 inner join
       [RC.Persons]
    on [RC.Persons_RC.CMO].person_id = [RC.Persons].person_id
 inner join
       [RC.FMO]
    on [RC.Persons_RC.FMO].fmo_id = [RC.FMO].fmo_id
  left join
       [RC.Appointments_RC.CMO]
    on [RC.CMO].cmo_id = [RC.Appointments_RC.CMO].cmo_id
  left join
       [RC.Appointments]
    on [RC.Appointments_RC.CMO].appointment_id = [RC.Appointments].appointment_id
 WHERE [RC.CMO].cmo_id = @cmo_id