Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - SQL Help
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

SQL Help

 Post Reply Post Reply
Author
fatcity101 View Drop Down
Newbie
Newbie


Joined: 11 November 2003
Status: Offline
Points: 5
Post Options Post Options   Thanks (0) Thanks(0)   Quote fatcity101 Quote  Post ReplyReply Direct Link To This Post Topic: SQL Help
    Posted: 11 November 2003 at 10:03am

Hello all,

I'm having some difficulty with finding members not in a group. For example, lets say we have an authors table, a books table and a book category table. I want to find all of the authors that have not written a book of a certain category. Since an author can have books in several categories, the typical

SELECT FROM (Books INNER JOIN Authors) INNER JOIN BookCategory WHERE BookCategory <> 'SciFi';

will not work. Is there an easier way to find what I'm looking for without looking through the recordset for members in the BookCategory = 'SciFi' list?

 



Edited by fatcity101
Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post Posted: 11 November 2003 at 12:34pm

Edit

WHERE BookCategory <> 'SciFi';

To

WHERE NOT BookCategory = 'SciFi';

I think thats what you mean

Back to Top
fatcity101 View Drop Down
Newbie
Newbie


Joined: 11 November 2003
Status: Offline
Points: 5
Post Options Post Options   Thanks (0) Thanks(0)   Quote fatcity101 Quote  Post ReplyReply Direct Link To This Post Posted: 11 November 2003 at 3:20pm

That won't work because an author could have written a SciFi book as well as a Non-Fiction book. If he has, he will still show up in the recordset because of his Non-Fiction book.

I'm looking for the group of authors that have never written a SciFi book (without having to requery the results of my first query looking for authors that have written a 'SciFi' book).

Sorry if I'm not explaining too well, I'm kinda new at this.

 

Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 11 November 2003 at 8:15pm

fatcity, your SQL Query is not a SQL Query and cannot work like that at all not to talk about showing the correct data.

Anyway, do you Access or SQL Server?
The problem with Access in that case is that Access has problem using OUTER Joins, which would be required. As you want to display Authors that have no Book that is part of a Cartegory that does exist you would go OUTER. So let me know what db u use.

Back to Top
fatcity101 View Drop Down
Newbie
Newbie


Joined: 11 November 2003
Status: Offline
Points: 5
Post Options Post Options   Thanks (0) Thanks(0)   Quote fatcity101 Quote  Post ReplyReply Direct Link To This Post Posted: 12 November 2003 at 7:00am

Let me clarify a little ...

There is an ASP page with a SQL statement that is querying an Access database. The query that I used to find the authors without SciFi books looks like this:

SELECT FirstName, LastName FROM (Authors as a INNER JOIN Books as b ON a.AuthorID = b.AuthorID) INNER JOIN BookCategory as c ON b.CategoryID = c.CategoryID WHERE c.Category <> 'SciFi'

I had shortened it in my original post for simplicity, but as I stated this will not work if an author has a SciFi book and a Non-Fiction book. The author will show up in the resulting recordset.

I am looking for a way to find all of the authors that have never written a SciFi book without having to look through the results with a second query. If you need any more info, let me know ... I appreciate the assistance.

Back to Top
ljamal View Drop Down
Mod Builder Group
Mod Builder Group


Joined: 16 April 2003
Status: Offline
Points: 888
Post Options Post Options   Thanks (0) Thanks(0)   Quote ljamal Quote  Post ReplyReply Direct Link To This Post Posted: 12 November 2003 at 8:34am
you need to add an additional clause to the query which removes the authors that have authored SciFi books and non SciFi. Your current query just removes the books that are non SciFi and doesn't take into account authors that have written multiple books one which at least one is SciFi and at least one non SciFi.
Back to Top
fatcity101 View Drop Down
Newbie
Newbie


Joined: 11 November 2003
Status: Offline
Points: 5
Post Options Post Options   Thanks (0) Thanks(0)   Quote fatcity101 Quote  Post ReplyReply Direct Link To This Post Posted: 12 November 2003 at 9:20am

I'm new at this so it's the language of that additional clause that I'm stumped on. Any suggestions?

Edit: Actually I think I've got it by adding:

AND a.AuthorID NOT IN (SELECT AuthorID FROM Books as bk INNER JOIN BookCategory as ct ON bk.CategoryID = ct.CategoryID WHERE ct.Category = 'SciFi')

It's long, but it works ... although I'm open to suggestions on optimization



Edited by fatcity101
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.