Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

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:
  #1  
Old September 20th, 2011, 12:39 PM
Lori J Lori J is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2011
Posts: 8 Lori J User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 48 m 3 sec
Reputation Power: 0
How do I retrieve results of this but not that?

I have a table "WR" that houses client information. I have another table "TR" that house information about services. I have a field "services" in the "TR" table that can have many codes if the client has received those services.

I would like to pull a query that shows me if the client has been in 1, 2, 3, 4 # codes BUT NOT IN 5. Is this possible?
Right now I would use the IN function to get the services I wanted to see, but if that client has received a 5 at any time, I don't want to see the client record at all.

Reply With Quote
  #2  
Old September 20th, 2011, 01:14 PM
Vomster's Avatar
Vomster Vomster is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2005
Posts: 724 Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 2 h 26 m 31 sec
Reputation Power: 290
Show your query

Reply With Quote
  #3  
Old September 20th, 2011, 01:20 PM
Lori J Lori J is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2011
Posts: 8 Lori J User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 48 m 3 sec
Reputation Power: 0
Quote:
Originally Posted by Vomster
Show your query



select wr.last_name,
wr.first_name,
tr.service,
tr.begin_date
from wr inner join tr on wr.id=tr.id
where tr.service IN ('1','2','3','4')

These shows me all clients who have had a service of 1 2 3 4. Which is great. But If they have had a service 5, I don't want to see any of their records.

Reply With Quote
  #4  
Old September 20th, 2011, 03:24 PM
Vomster's Avatar
Vomster Vomster is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2005
Posts: 724 Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 2 h 26 m 31 sec
Reputation Power: 290
Code:
SELECT
	wr.last_name,
	wr.first_name,
	tr.service,
	tr.begin_date
FROM wr 
INNER JOIN tr ON wr.id=tr.id
WHERE
	tr.service IN ('1','2','3','4') 
	AND wr.id NOT IN 
		(SELECT id 
			FROM tr 
			WHERE tr.service = '5'
		)

Reply With Quote
  #5  
Old September 20th, 2011, 04:33 PM
Lori J Lori J is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2011
Posts: 8 Lori J User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 48 m 3 sec
Reputation Power: 0
AWESOME! It worked perfectly. And makes complete sense. I have never worked with a subquery before and this helps put a lot of connections in place.

I appreciate the help so much!

Reply With Quote
  #6  
Old September 21st, 2011, 07:37 AM
Vomster's Avatar
Vomster Vomster is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2005
Posts: 724 Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level)Vomster User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 2 h 26 m 31 sec
Reputation Power: 290
Great

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > How do I retrieve results of this but not that?

Developer Shed Advertisers and Affiliates



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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap