SunQuest
           Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old July 24th, 2003, 02:46 PM
don_sparko's Avatar
don_sparko don_sparko is offline
Digitally Challenged
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 280 don_sparko User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 m 14 sec
Reputation Power: 6
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

Reply With Quote
  #2  
Old July 24th, 2003, 03:11 PM
messorian messorian is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: NY
Posts: 18 messorian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 18 sec
Reputation Power: 0
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 

Reply With Quote
  #3  
Old July 24th, 2003, 03:15 PM
messorian messorian is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: NY
Posts: 18 messorian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 18 sec
Reputation Power: 0
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 

Reply With Quote
  #4  
Old July 28th, 2003, 08:23 AM
don_sparko's Avatar
don_sparko don_sparko is offline
Digitally Challenged
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 280 don_sparko User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 m 14 sec
Reputation Power: 6
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!

Reply With Quote
  #5  
Old July 28th, 2003, 11:08 AM
don_sparko's Avatar
don_sparko don_sparko is offline
Digitally Challenged
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 280 don_sparko User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 m 14 sec
Reputation Power: 6
i got it, left outer join is the ticket!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Getting records using joins, but also getting those with nulls


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway