tags:

views:

43

answers:

2

I have two tables (Releases and Formats) and a field that is linked to both of them - bar_code. The problem I am having is with this select statement:

SELECT
    Release.name,
    Release.default_upc,
    Artist.name,
    Artist.url_name,
    Format.*
FROM
    releases AS Release,
    artists AS Artist,
    formats AS Format
WHERE
    Release.id IN(20015, 2414) AND
    Artist.id = Release.artist_id AND
    Format.bar_code = Release.default_upc

The issue is that when Release.default_upc = null, no record will be returned. However, it is a valid behavior that default_upc can be null, and if it is the query should not attempt to look for a Format with Format.bar_code = null - instead just returning the other selected data.

Not actually sure this is 100% possible in MySQL, but open to any advice.

+3  A: 

Use Outer Joins.

One site that explains

Another one

Dani
+1  A: 

Use an outer join...here's a quick shot. Check the other links for explanation.

select Release.name,
       Release.default_upc,
       Artist.name,
       Artist.url_name,
       Format.*
from releases as Release
inner join artists as Artist
    on Artist.id = Release.artist_id
outer join formats as Format
    on Format.bar_code = Release.default_upc
where
    Release.id in (20015, 2414)
Justin Niessner