0

I'm trying to select all my areas and join the deals for those areas... this query is returning 0 results when I have verified that I have data to match what I'm expecting to get... does anyone see anything obviously wrong?

SELECT
                deal.*,
                area.id AS area_id
            FROM area
            INNER JOIN account_areas ON (
                account_areas.account_id = 1 AND
                account_areas.area_id = area.id
            )
            JOIN deal ON (
                deal.area_id = area.id AND
                deal.site_id = 1 AND
                DAYOFYEAR(deal.created) = DAYOFYEAR(NOW()) AND
                deal.end >= NOW()
            ) 
            ORDER BY area.name ASC

The idea is that I want to pull all deals for a certain area, but if there are no deals, still have the area in the result query.

account
---------------------------------
|    id    |        email       |
---------------------------------
|    1     |   test_test.com    |
---------------------------------

account_areas
------------------------
| account_id | area_id |
------------------------
|      1     |   81    |
|      1     |   42    |
------------------------

deal
--------------------------------------------------------
|    id    |  area_id  |  Title                        |
--------------------------------------------------------
|    1     |     81    |    Test Title                 |
--------------------------------------------------------

Expected results WITH a deal:

id | area_id | title

Expected results WITHOUT a deal

area_id
2
  • 2
    Can you provide an excerpt of the table involved? That way we can recreate the database you are using (or a portion thereof) and start troubleshooting the query. Also, are you able to get any results with just one of the two JOINs? As in, does the INNER JOIN work on it's own? Or the "JOIN deal"? Sep 2, 2011 at 1:40
  • When your question involves "not expected results", you should always post what you expect the results to be... along as, of course, as much information you have to help answer it.
    – netcoder
    Sep 2, 2011 at 1:46

1 Answer 1

0

Use a ...LEFT JOIN DEAL... which will return nulls for all DEAL columns if there are no deals.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.