User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 374,009 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,893 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser:
Views: 423 | Replies: 3 | Solved
Reply
Join Date: Apr 2007
Location: Birmingham
Posts: 368
Reputation: Fungus1487 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 35
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Whiz

Confused?

  #1  
Mar 20th, 2008
Hello all,

hope i can get some help on this one as im not brilliant with my database scripts.

I have table "A" and table "B", table "A" contains information on a portfolio and table "B" is a joining table which connects this table to a user of the system with a 1-to-many relationship.


I am looking to return all records in table "A" that are not connected to a user in table "B"

i have tried this

SELECT a.* FROM a LEFT JOIN b ON a.id = b.id WHERE b.userid <> 1

but i know this is incorrect but cannot figure out myself how to achieve what i want.

Hopefully someone can point me in the right direction.
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Location: Jogja
Posts: 2,330
Reputation: Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light 
Rep Power: 9
Solved Threads: 204
Jx_Man's Avatar
Jx_Man Jx_Man is offline Offline
Nearly a Posting Maven

Re: Confused?

  #2  
Mar 21st, 2008
Select * from Table1 a left join Table2 b on (a.id = b.id) where a.id <> '1'
Last edited by Jx_Man : Mar 21st, 2008 at 3:05 am.
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
Reply With Quote  
Join Date: Apr 2007
Location: Birmingham
Posts: 368
Reputation: Fungus1487 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 35
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Whiz

Re: Confused?

  #3  
Mar 21st, 2008
Originally Posted by Jx_Man View Post
Select * from Table1 a left join Table2 b on (a.id = b.id) where a.id <> '1'


sorry this does not work.
It simply filters table A's records removing the one with an id of '1'.



its probably easier with a working example.

I have the following tables...

### 'portfolio' - table
'portfolioid' - int [primary key]
'usertitle' - string
'clienttitle' - string

### Records
1, "test title", "test title"
2, "test title 1", "test title a"
3, "test title 2", "test title b"
4, "test title 3", "test title c"

### 'portfolioclient' - table
'portfolioid' - int
'clientid' - int

### Records
1, 1
3, 1
2, 2
4, 2

i wish to return all portfolio records which are not attached to a 'clientid'. E.G. if a client has an ID of '1' then the portfolio records returned should be 2 and 4.

i thought this would work but obviosuly not.
SELECT portfolio.* FROM portfolio LEFT JOIN portfolioclient ON portfolio.portfolioid = portfolioclient.portfolioid WHERE portfolioclient.clientid <> 1
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote  
Join Date: Apr 2007
Location: Birmingham
Posts: 368
Reputation: Fungus1487 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 35
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Whiz

Re: Confused?

  #4  
Mar 21st, 2008
Ok managed to solve this one myself after a bit of staring at my screen for a long time. For anyone interested this worked for me...

SELECT portfolio.portfolioid FROM portfolio WHERE (NOT EXISTS (SELECT portfolioclient.* FROM portfolioclient WHERE (portfolio.portfolioid = portfolioclient.portfolioid) AND (portfolioclient.clientid = 1)))
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MS SQL Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 10:42 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC