LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-30-2007, 03:49 AM   #1
matthewhardwick
Member
 
Registered: Oct 2003
Location: CA
Posts: 165

Rep: Reputation: 30
Post Wierd SQL query selecting from table acording another tables lack of values?


I want to select all the rows from table1 that have keys that are not listed in a field in table2.

In it's simplist form:

Table 1 is a list of Books.
Table 2 is a list of Book IDs and Subject IDs

I want to select all the books that don't have a subject, the simplest way I can tell of doing this is to list all the Titles in table 1 who's keys are not mention in the book id column of table 2.

I am pretty sure the SQL here is fairly standard, just never come across this before.

If it helps I am using Oracle 8i (sorry not my choice).

TIA

Matt.
 
Old 07-30-2007, 04:04 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well assuming that Table 1 contains a title and an ID, then I would do a left join on the two tables as a starting point. now this looks a hell of a lot like coursework or something, so i'm not going to give you a complete solution... if it's not and you still need more assistance please clarify your situation.
 
Old 07-30-2007, 04:10 AM   #3
matthewhardwick
Member
 
Registered: Oct 2003
Location: CA
Posts: 165

Original Poster
Rep: Reputation: 30
lol course work? I work for a Library not done "course work" in a while. If it was coursework, then I'd have a clue as to what I was doing, because we would have been tought it!


I am working with a database that I am not allowed to change, I just have to run reports on it etc... and this one has me stumped.

There isn't anything more to clarify really... what do you want to know?
 
Old 07-30-2007, 05:51 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
ok, i'll take your word for it then...

so as above it's a LEFT JOIN you want. which takes all the data in the left side table of the expression "SELECT * FROM TABLEA LEFT JOIN TABLEB" and matches it up with matching records in the right side table. if there is no matching data, i.e. no subject in your case, then the data is null for those fields. so the other part of the issue is to just select from the query rows where is description is null, e.g. "SELECT * FROM TABLEA LEFT JOIN TABLEB WHERE TABLEB.DESCRIPTION IS NULL"
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
In Memory SQL type tables koodoo Programming 2 08-05-2006 03:55 AM
read values of variables from a table osio Programming 7 01-11-2006 02:52 AM
SQL: tables and queries in school project cold Programming 1 10-29-2005 07:58 PM
Multiple values with SQL (Noob stuff) Travis86 Programming 6 09-03-2003 08:38 PM
SQL query, comparing tables ngomong Programming 3 07-07-2002 07:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration