tags:

views:

38

answers:

2

Hello everyone:

I have a query that works well, but I'm trying to see if I can clean up my results a little bit.

The query pulls the State and Fiber ID fields from my table called fiber_intake. It then pulls all fields from my hvi table where the Fiber ID = GA in the fiber_intake table.

Code:

SELECT `fiber_intake`.`State`, `fiber_intake`.`Fiber ID`, `hvi`.*
FROM `fiber_intake`
 LEFT JOIN `hvi` ON `fiber_intake`.`Fiber ID` = `hvi`.`Fiber ID` 
WHERE (`fiber_intake`.`State` = 'GA')

It works great, except it pulls all the GA Fiber ID's, even the one's without hvi data in the other table. So I get some results with a bunch of Nulls for the hvi data.

Is there a way to exclude the Fiber ID's that do not have hvi table data associated with them?

Thanks!

+6  A: 

Use an INNER JOIN instead of a LEFT [OUTER] JOIN.

This adding back of rows is the whole purpose of an OUTER JOIN.

SELECT `fiber_intake`.`State`, `fiber_intake`.`Fiber ID`, `hvi`.*
FROM `fiber_intake`
 INNER JOIN `hvi` ON `fiber_intake`.`Fiber ID` = `hvi`.`Fiber ID` 
WHERE (`fiber_intake`.`State` = 'GA')
Martin Smith
You beat me by 6 seconds.
Phil Sandler
+3  A: 

Use an INNER JOIN.

Phil Sandler