0

I want to select some data from different tables with some primary and foreign key links.

The AppointmentID is not necessary but when there is no link betweeen RC.CMO and RC.Appointments_RC.CMO, the select give me a null result, cause the "where" is not satisfied.

How can I select all data, also when there is no appointment for the cmo?

I tink that is the problem:

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

I cant find some data if the person is not linked to an appointment.

Statement:

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

Is it possible to make an exists OR not exists?

2
  • 3
    try to create a left join Jan 28, 2014 at 12:33
  • 1
    Use JOIN syntax. This is required to have a left join, and you should do it anyway because this mess is incomprehensible.
    – usr
    Jan 28, 2014 at 12:39

2 Answers 2

3

If you convert your query to use the modern explicit JOIN syntax instead of the old-fashioned implict one, you can have OUTER joins, which do exactly what you want:

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
3
  • Old timers also had OUTER JOINS using *= and =* ;-)
    – StuartLC
    Jan 28, 2014 at 13:11
  • @StuartLC Was not aware of that syntax. Thanks!
    – Mureinik
    Jan 28, 2014 at 13:15
  • I need to left join the link between [RC.Appointments_RC.CMO].appointment_id = [RC.Appointments].appointment_id and and it works!
    – mnlfischer
    Jan 28, 2014 at 13:18
1

try this. i removed duplicate tables and optimised the joins:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.