Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Help with SQL query

Author  Topic 

ProEdge
Yak Posting Veteran

64 Posts

Posted - 2006-03-03 : 23:44:12
Hi everyone,
okay so I have a somewhat complex SQL query that I'm trying to write. Or at least it's complex for me. I'll draw out the two tables that I have:

BookTable
=========
book_id (PK)
author_id (FK)
title

ReviewTable
===========
book_id (FK)
author_id (FK)
comment

Both of these have more columns in them but I didn't include them since they're not really relevant. Don't worry about the author_id. It is coming from its own table as well. Okay now let me include some sample data:

BookTable
=========
book_id author_id title
1 1 Of Mice and Men
2 2 Clockwork Orange
3 1 The Wizard of Oz
4 3 Beyond the Infinite

ReviewTable
===========
book_id author_id comment
1 2 blah blah
2 3 some comment
3 2 wow!
4 1 great book

Okay so the Book table shows us a list of books and their respective authors. The Review table, on the other hand, shows us the same books listed in the book table but which have been reviewed by other authors. So for instance, author_id #2 has reviewed both book #1 (Of Mice and Men) and book #3 (The Wizard of Oz). What I'd like to do now is return the books from the Book table which have not been reviewed by a specific author. So in the example I just gave, the books that would be returned for author_id #2 would only include book #2 (Clockwork Orange) and book #4 (Beyond the Infinite). I hope I explained this clearly but if anyone still has more questions, please feel free to ask. Thanks for your help!

t1g312
Posting Yak Master

148 Posts

Posted - 2006-03-04 : 01:02:38
select b.Title from ReviewTable r left outer join BookTable b on r.book_id = b.book_id where r.author_id <> 2

quote:
Originally posted by ProEdge

Hi everyone,
okay so I have a somewhat complex SQL query that I'm trying to write. Or at least it's complex for me. I'll draw out the two tables that I have:

BookTable
=========
book_id (PK)
author_id (FK)
title

ReviewTable
===========
book_id (FK)
author_id (FK)
comment

Both of these have more columns in them but I didn't include them since they're not really relevant. Don't worry about the author_id. It is coming from its own table as well. Okay now let me include some sample data:

BookTable
=========
book_id author_id title
1 1 Of Mice and Men
2 2 Clockwork Orange
3 1 The Wizard of Oz
4 3 Beyond the Infinite

ReviewTable
===========
book_id author_id comment
1 2 blah blah
2 3 some comment
3 2 wow!
4 1 great book

Okay so the Book table shows us a list of books and their respective authors. The Review table, on the other hand, shows us the same books listed in the book table but which have been reviewed by other authors. So for instance, author_id #2 has reviewed both book #1 (Of Mice and Men) and book #3 (The Wizard of Oz). What I'd like to do now is return the books from the Book table which have not been reviewed by a specific author. So in the example I just gave, the books that would be returned for author_id #2 would only include book #2 (Clockwork Orange) and book #4 (Beyond the Infinite). I hope I explained this clearly but if anyone still has more questions, please feel free to ask. Thanks for your help!



Adi
Go to Top of Page

ProEdge
Yak Posting Veteran

64 Posts

Posted - 2006-03-04 : 01:40:44
Hey thanks! It worked great. I knew I had to use a join some how but wasn't sure. Thanks again!
Go to Top of Page

ProEdge
Yak Posting Veteran

64 Posts

Posted - 2006-03-04 : 20:03:51
Hi,
I just wanted to post a follow up to this question. I'm running the current SQL that you gave me which is:

Select b.Title
From ReviewTable r left outer join BookTable b
On r.book_id = b.book_id
Where r.author_id <> 2

Now there is one more thing that I'd like to pull out at the same time. In addition to returning the books from the Book table which have not been reviewed by a specific author, I'd like to return those books which where not written by the author. I tried adding the following to my SQL but it obviously doesn't work:

Select b.Title
From ReviewTable r left outer join BookTable b
On r.book_id = b.book_id
Where r.author_id <> 2
And
b.book_id = Any (Select book_id
From BookTable
Where author_id <> 2)

So essentially I'm checking to see that the books I'm retrieving from the BookTable do not belong to author_id #2. The problem with this statement is that it will still return books that were reviewed by the author. Thanks
Go to Top of Page

ProEdge
Yak Posting Veteran

64 Posts

Posted - 2006-03-04 : 20:23:42
Actually I think now the previous code does work!? I'm confused...lol What do you think?
Go to Top of Page

ProEdge
Yak Posting Veteran

64 Posts

Posted - 2006-03-04 : 20:44:34
Okay I made some modifications to the above SQL and it's working fine now. I just added a group by and having clause. I'll come back if I have further problems. Thanks
Go to Top of Page

ProEdge
Yak Posting Veteran

64 Posts

Posted - 2006-03-05 : 16:27:22
t1g312,
I seem to have found some kind of problem with the initial SQL you gave me. Let me add the following data to the original tables:
BookTable
=========
book_id author_id title
1 1 Of Mice and Men
2 2 Clockwork Orange
3 1 The Wizard of Oz
4 3 Beyond the Infinite

ReviewTable
===========
book_id author_id comment
1 2 blah blah
2 3 some comment
3 2 wow!
4 1 great book
4 4 excellent
4 8 didn't like it

So all I did was add two more rows into the ReviewTable, which were for book_id 4. Using the SQL statement you gave me:

Select b.Title
From ReviewTable r left outer join BookTable b
On r.book_id = b.book_id
Where r.author_id <> 1

I now replaced the author_id with #1. The returned books include:

1
2
3
4 <--This is the one with comment "excellent"
4 <--This is the one with comment "didn't like it"

It returns the book titles but I just replaced them with the book_id's better because it shows you that the returned values include the book_id #4 which was reviewed by author_id 1. However, it still returns the rest of the #4 books in the ReviewTable. Do you see what I mean?
Go to Top of Page

ProEdge
Yak Posting Veteran

64 Posts

Posted - 2006-03-06 : 17:44:52
*bump*
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-03-06 : 18:06:38
ProEdge,

In order for us to help you, we need the DDL for the tables (CREATE TABLE statements), INSERT INTO statements for your sample data, and the expected result set using that sample data. Once we have that, we can copy the code into our own machines and come up with a solution.

Tara Kizer
aka tduggan
Go to Top of Page

ProEdge
Yak Posting Veteran

64 Posts

Posted - 2006-03-06 : 21:06:13
Here is the script that I believe you need right?
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[booktable]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[booktable](
[book_id] [int] IDENTITY(1,1) NOT NULL,
[author_id] [int] NULL,
[title] [nvarchar](max) NULL,
CONSTRAINT [PK_booktable] PRIMARY KEY CLUSTERED
(
[book_id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reviewtable]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[reviewtable](
[book_id] [int] NULL,
[author_id] [int] NULL,
[comment] [nvarchar](max) NULL
) ON [PRIMARY]
END
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_reviewtable_booktable]') AND parent_object_id = OBJECT_ID(N'[dbo].[reviewtable]'))
ALTER TABLE [dbo].[reviewtable] WITH CHECK ADD CONSTRAINT [FK_reviewtable_booktable] FOREIGN KEY([book_id])
REFERENCES [dbo].[booktable] ([book_id])

I made this script based on the SQL Server 2000 DB, although I do have 2005. This is just a very simple example that I made on SQL Server. I'm actually using MySQL but have been testing on SQL Server. Don't worry about your SQL syntax not being MySQL compatible. I'll have to modify if I need to.
Go to Top of Page
   

- Advertisement -