Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-29-2007, 10:25 AM   PM User | #1
homerUK
Regular Coder

 
Join Date: Nov 2002
Location: Manchester, UK
Posts: 533
Thanks: 4
Thanked 1 Time in 1 Post
homerUK is an unknown quantity at this point
join tables to count number of records?

Hi,

I've two tables

responses
==========
response_id
schema_id
timestamp

answers
==========
answer_id
response_id
answer

(that's a cut down version, but will do for this question)

I need to get all the responses where there is at least one answer in the answers table. But I do not want the answer data. It's literally a quick check for an export to say "get me all the responses where there's at least one question answered".

I have this:

Code:
select
r.*
FROM responses r
JOIN task_answers ta on r.response_id = ta.response_id
WHERE r.schema_id = 13
ORDER BY r.response_id ASC
but that's returning all the rows in the answer table. I am not interested in getting those...

.... do I need a LEFT or RIGHT, OUTER or INNER join or something?

cheers for any help!!
__________________
www.mattfacer.com
homerUK is offline   Reply With Quote
Old 03-29-2007, 10:43 AM   PM User | #2
SSJ
Regular Coder

 
Join Date: Mar 2007
Posts: 230
Thanks: 0
Thanked 4 Times in 4 Posts
SSJ is an unknown quantity at this point
Really Didn't Get This...
__________________
Best Joomla Design Agency India ||PSD to Joomla Template || Award Winning Web Design Company India
SSJ is offline   Reply With Quote
Old 03-29-2007, 12:14 PM   PM User | #3
homerUK
Regular Coder

 
Join Date: Nov 2002
Location: Manchester, UK
Posts: 533
Thanks: 4
Thanked 1 Time in 1 Post
homerUK is an unknown quantity at this point
all I need to do is get all the records from TABLEA (responses) where there is at least one record in TABLEB (answers)
__________________
www.mattfacer.com
homerUK is offline   Reply With Quote
Old 03-29-2007, 01:34 PM   PM User | #4
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
count the records in table B only, there is no need to do anything with table A. Those questions in table A that haven't been answered won't exist in table B right?
guelphdad is offline   Reply With Quote
Old 03-29-2007, 01:36 PM   PM User | #5
homerUK
Regular Coder

 
Join Date: Nov 2002
Location: Manchester, UK
Posts: 533
Thanks: 4
Thanked 1 Time in 1 Post
homerUK is an unknown quantity at this point
yeah thats right, I never thought of turning it round like that...

I need to get the records from A though... they are being used... i just want to ignore any records in A that dont have entries in B....
__________________
www.mattfacer.com
homerUK is offline   Reply With Quote
Old 03-29-2007, 01:39 PM   PM User | #6
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
An outer join pulls all records from one table and matches up rows from the second table, inserting NULLs where there is no matching record.

Code:
select
r.*
FROM responses r
LEFT OUTER JOIN 
task_answers ta 
ON r.response_id = ta.response_id
AND ta.response_id IS NOT NULL
WHERE r.schema_id = 13
ORDER BY r.response_id ASC
guelphdad is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:58 AM.


Advertisement
Log in to turn off these ads.