查询未返回预期结果

时间:2011-09-02 01:36:53

标签: mysql

我正在尝试选择我的所有区域并加入这些区域的交易...当我确认我的数据与我期望得到的数据相符时,此查询返回0结果...是否有人看到任何明显错误的东西?

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

我的想法是,我希望为特定区域提取所有交易,但如果没有交易,则仍会在结果查询中包含该区域。

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

预期结果与交易:

id | area_id | title

没有交易的预期结果

area_id

1 个答案:

答案 0 :(得分:0)

使用...LEFT JOIN DEAL...,如果没有交易,将为所有DEAL列返回空值。