473,382 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

3 table join

Hi,

I've got 3 tables:
1.) book: BookID, Title
2.) bookcomment: BookCommentID, BookID, ContactID, Comment
3.) responsibilitylog: ResponsibilityID, BookID, ContactID, DataFrom, DateTo


Relationships:
book and bookcomment: one to one
book and responsibilitylog: one to many

I want a query that returns book.Title and bookcomment.Comment WHERE BookID=1 AND ContactID=2 regardless if responsibilitylog returns a record,
AND
the query must return all the records with fields book.Title, responsibilitylog.DataFrom and responsibilitylog.DateTo WHERE BookID=1 AND ContactID=2 regardless if bookcomment returns a record

The following query only returns data if there is a record in bookcomment AND responsibilitylog that meet the conditions because of my AND statement, but I don't know how to rectify this. If I use OR in place of AND, I do not get the desired result.

Expand|Select|Wrap|Line Numbers
  1. rs = st.executeQuery("select b.Title, rl.DateFrom, rl.DateTo, c.Comment from book b left join responsibilitylog rl on b.BookID = rl.BookID left join bookcomment c on c.BookID = b.BookID WHERE rl.ContactID='" + ContactID + "' AND c.ContactID='" + ContactID + "'");
  2.  
Jun 19 '08 #1
8 1756
code green
1,726 Expert 1GB
regardless if bookcomment returns a record
Then it needs to be LEFT JOIN
Jun 19 '08 #2
Thanks for your response,

I changed it to left join, but still:

The query only returns data if there is a record in bookcomment AND responsibilitylog that meet the conditions. I have data in responsibilitylog table that should be returned, but for that ContactID and BookID, there is no record in the bookcomment table and therefore it doesn't return the data from the responsibilitylog table.
Jun 19 '08 #3
henryrhenryr
103 100+
Expand|Select|Wrap|Line Numbers
  1. rs = st.executeQuery("select b.Title, rl.DateFrom, rl.DateTo, c.Comment from book b left join responsibilitylog rl on b.BookID = rl.BookID left join bookcomment c on c.BookID = b.BookID WHERE rl.ContactID='" + ContactID + "' AND c.ContactID='" + ContactID + "'");
  2.  
At a glance I think you can work around either:

1. Input default settings - when you add a book to your DB, always add records for comment and responsibility, but add default/null rows. Then you don't have the problem.

2. Probably preferable, I think you should be able to use AND (... OR ...)

Expand|Select|Wrap|Line Numbers
  1. select b.Title, rl.DateFrom, rl.DateTo, c.Comment 
  2. from book b 
  3.   left join responsibilitylog rl on b.BookID = rl.BookID 
  4.   left join bookcomment c on c.BookID = b.BookID 
  5. WHERE (rl.ContactID='"+CONTACTID+"' OR rl.ContactID IS NULL)
  6.    AND (c.ContactID='"+CONTACTID+"' OR c.ContactID IS NULL)
  7.  
or perhaps you're looking for this...

Expand|Select|Wrap|Line Numbers
  1. ...
  2. WHERE (rl.ContactID='"+CONTACTID+"' AND c.ContactID='"+CONTACTID+"') 
  3.    OR (rl.ContactID IS NULL AND c.ContactID IS NULL)
  4.  
Haven't tested though... ;)

Henry
Jun 19 '08 #4
Thanks for you input Henry, you've given me some ideas to try out. I have tried various options concerning AND OR. The following example comes pretty close to what I want, except that it returns data from the bookcomment table where the ContactID is not equal to the one that I've specified. I suspect it is because of the way that the joins are made:

Expand|Select|Wrap|Line Numbers
  1. select b.BookID, b.Title, b.Author, rl.DateFrom, rl.DateTo, c.Comment from book b left join responsibilitylog rl on b.BookID = rl.BookID left join bookcomment c on c.BookID = b.BookID WHERE (rl.ContactID='"+ContactID+"' OR rl.ContactID IS NULL) OR (c.ContactID='"+ContactID+"' AND c.BookID='"+BookID+"')
I'm basically trying to combine the following 2 queries:
1.)
Expand|Select|Wrap|Line Numbers
  1. "SELECT b.BookID, b.Title, b.Author, rl.DateFrom, rl.DateTo FROM book b LEFT JOIN responsibilitylog rl ON b.BookID=rl.BookID WHERE rl.ContactID=" + ContactID + " ORDER BY Title"
2.)
Expand|Select|Wrap|Line Numbers
  1. "SELECT b.BookID, b.Title, b.Author, c.Comment FROM book b LEFT JOIN bookcomment c ON b.BookID=c.BookID WHERE c.ContactID=" + ContactID + " ORDER BY Title"
Jun 20 '08 #5
code green
1,726 Expert 1GB
The following example comes pretty close to what I want, except that it returns data from the bookcomment table where the ContactID is not equal to the one that I've specified. I suspect it is because of the way that the joins are made:
Maybe your second LEFT JOIN needs to be an INNER JOIN.
Expand|Select|Wrap|Line Numbers
  1. INNER JOIN bookcomment c ON c.BookID = b.BookID 
You can try putting WHERE conditions inside the ON clause. Not sure where you are going with this but I mean
Expand|Select|Wrap|Line Numbers
  1. JOIN bookcomment c ON (c.BookID = b.BookID AND c.BookID='"+BookID+"')
Jun 20 '08 #6
Thanks for your help code green ,

I am one step closer with the following:

Expand|Select|Wrap|Line Numbers
  1. "select b.BookID, b.Title, b.Author, rl.DateFrom, rl.DateTo, c.Comment from book b left join responsibilitylog rl on b.BookID = rl.BookID left join bookcomment c on (b.BookID = c.BookID AND c.ContactID='"+ContactID+"') WHERE (rl.ContactID='"+ContactID+"' OR rl.ContactID IS NULL) OR (c.ContactID='"+ContactID+"' AND c.BookID='"+BookID+"')"
  2.  
Jun 24 '08 #7
coolsti
310 100+
I am just glancing at this thread without spending the time to solve the problem, but I do not think it is very difficult to do (maybe I have time later and will repost).

But here a comment: The Book and Bookcomment table are 1 to 1 and, indeed, could actually be combined to form one database table. Therefore I would do the query by joining these two tables first, and then doing a left join on the Responsibility table.

In all the examples I have seen so far, you have first joined the Book and Responsibility table, and then joined the Bookcomment table.
Jun 26 '08 #8
coolsti
310 100+
Try this, it works for me (although I used different table definitions, so if you get a syntax error with what I show below, you may need to modify to fix it).

select b.BookID, b.Title, b.Author, rl.DateFrom, rl.DateTo, c.Comment
from book b left join bookcomment c on b.BookID=c.BookID
left join responsibilitylog rl on b.BookID=rl.BookID
having b.BookID=1 and
((c.ContactID=2 or c.ContactID is NULL) and
(rl.ContactID=2 or rl.ContactID is NULL))

First try this without the having clause. You will see that you get a join of all possibilities: all book ID's and their associated rows in the two other tables. The HAVING clause is then used to filter out the ones you don't wish. The filter is of course over the book ID, and then two other conditions:

1) either the bookcomment ContactID is the value you are looking for or it is NULL (the row is not present for this book in the bookcomment table)

2) either the responsibilitylog ContactID is the value you are looking for, or is NULL (the row is not present for this book in the responsibilitylog table.

Try this. I think it is what you need. Oh, of course you must substitute the "1" and the "2" in my example for whatever variables you are using.
Jun 27 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Mahesh Hardikar | last post by:
Hi , Oracle 8.1.7.0.0 on HP-UX 11.0 I am a newbie as far as PL-SQL is concerned . I have Sybase/MSSQL T-SQL background. We have a report which uses a select statement . This select...
0
by: B. Fongo | last post by:
I learned MySQL last year without putting it into action; that is why I face trouble in formulating my queries. Were it a test, then you would have passed it, because your queries did help me...
1
by: Stephen Patten | last post by:
Hi All, While in the process of building my table (40 or so Insert statments) can I then query ("select * from @Table_variable") and use the results up to theat point for another insert into...
1
by: Sascha | last post by:
Hi I need some help on achieving the following: I wrote a querie which collects Data out of three Tables, the Result looks like this: SET NOCOUNT ON DECLARE @ROW INT
2
by: Mike Leahy | last post by:
Hello all, This question is related to updating tables - is there any way to calculate or update the values in a column in a table to the values in a field produced by a query result? An...
4
by: Fred | last post by:
Hi. What is the usual and what are the possible fields to use for the primary key of an intersecting table of a many-to-many relationship? I would think the typical, most common fields would...
2
by: Thomas R. Hummel | last post by:
Hello, I am currently working on a monthly load process with a datamart. I originally designed the tables in a normalized fashion with the idea that I would denormalize as needed once I got an...
3
by: das | last post by:
Hello all, Can someone help me with this SQL? 1) EMPLOYEE table has columns 'employee_id' and 'emp_sid' and other columns 2) EMPLOYEE_BENEFITS table has a column called employee_entity, this...
14
by: John Salerno | last post by:
Hi guys. I was wondering if someone could suggest some possible structures for an "Education" table in a database. It will contain at minimum university names, years of attendance, and degrees...
3
by: angelnjj | last post by:
I'm going to do my best to describe purpose and what I need...here goes. I'm writing a data entry "quality" report and working. I'm trying to identify anytime a user adds a new contact to the db...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.