|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
||||
|
||||
|
Getting records using joins, but also getting those with nulls
ok, so here i am doing a query to get some info off a table, and i decide that i want to fill a name in using a join to the end user sees a name and not a number. now for the tricky part. there are null values in this field, or at least there can be. so naturally it only returns the records that have a number there, when i really want all of them, and just display the name if it exits. here is what i have:
select b.l_name, b.f_name, a.keyedinby, c.l_name, c.f_name, a.keyedoutby, a.datein, a.dateout, a.dateperiod from employee as b, employee as c, tracklog as a where custid = 16 and a.keyedinby = b.emp_id and a.keyedoutby = c.emp_id and a.dateperiod between 'date1' and 'date2' order by a.dateperiod desc it works like it should without the statemensts that contain c like this select b.l_name, b.f_name, a.keyedinby, a.keyedoutby, a.datein, a.dateout, a.dateperiod from employee as b, tracklog as a where custid = 16 and a.keyedinby = b.emp_id and a.dateperiod between 'date1' and 'date2' order by a.dateperiod desc so any ideas? questions? oh yeah i'm using MSSQL7 |
|
#2
|
|||
|
|||
|
Check out books online and look up the IsNull function.
-- messorian Basically, your procedure should look like this: Code:
SELECT IsNull(b.l_name, ''), IsNull(b.f_name, ''), a.keyedinby, IsNull(c.l_name, ''), IsNull(c.f_name, ''), a.keyedoutby, a.datein, a.dateout, a.dateperiod FROM employee as b, employee as c, tracklog WHERE custid = 16 And tracklog.keyedinby = b.emp_id And tracklog.keyedoutby = c.emp_id And tracklog.dateperiod between 'date1' and 'date2' ORDER BY tracklog.dateperiod DESC |
|
#3
|
|||
|
|||
|
Sorry, I misread, try this code:
-- messorian Code:
SELECT b.l_name, b.f_name, a.keyedinby, c.l_name, c.f_name, a.keyedoutby, a.datein, a.dateout, a.dateperiod FROM tracklog LEFT OUTER JOIN employee as b On tracklog.keyedinby = b.emp_id LEFT OUTER JOIN employee as c On tracklog.keyedoutby = c.emp_id WHERE custid = 16 And tracklog.dateperiod between 'date1' and 'date2' ORDER BY tracklog.dateperiod DESC |
|
#4
|
||||
|
||||
|
thanks for the replies, but just wondering where you are reverencing the a.column name. i need that too, so whatever you can muster will be appreciated. thanks!
|
|
#5
|
||||
|
||||
|
i got it, left outer join is the ticket!
|
![]() |
| Viewing: Dev Shed Forums > Databases > Database Management > Getting records using joins, but also getting those with nulls |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|