sql – How can I not display null values in left join results? – Education Career Blog

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!

,

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')

,

Use an INNER JOIN.

Leave a Comment