MySQL Left Join 未产生预期结果
我知道左连接上有很多很好的问题得到解答,但我无法找到与我的特定问题相关的内容。对于可能重复的话题,我深表歉意,但我们将不胜感激。
我有两个包含礼品卡销售的表。我必须核实金额是否相符。 我正在寻求帮助编写一个查询,即使表 B 中没有匹配的记录,该查询也会返回表 A 中的所有内容。
表 A
| id | business_date | am_pm | gift_cards_sold |
================================================
103 | 2011-10-06 | PM | 175.03
104 | 2011-10-06 | PM | 135.03
105 | 2011-10-06 | PM | 250.74
106 | 2011-10-06 | PM | 180.44
107 | 2011-10-06 | PM | 150.10
108 | 2011-10-06 | PM | 130.00
表 B
| id | business_date | am_pm | gift_cards_sold |
================================================
103 | 2011-10-06 | PM | 100.03
105 | 2011-10-06 | PM | 250.74
106 | 2011-10-06 | PM | 180.44
107 | 2011-10-06 | PM | 150.10
这是到目前为止我的查询(显然有问题)
SELECT A.id AS ID, A.gift_cards_sold AS A_SOLD, B.gift_cards_sold AS B_SOLD
FROM A
LEFT JOIN B
USING (id)
WHERE A.am_pm = 'PM'
AND A.business_date = '2011-10-06'
AND B.business_date = '2011-10-06'
GROUP BY A.id
ORDER BY A.id ASC
是结果:
| id | A_SOLD | B_SOLD |
========================
103 | 175.03 | 100.03
105 | 250.74 | 250.74
106 | 180.44 | 180.44
107 | 150.10 | 150.10
这 你可以看到,id 为 103 存在差异。但是,我需要让结果显示每个 id,无论是否存在匹配。结果集是我期望使用内连接得到的结果。
I know there is a lot of good questions answered on Left Joins, but I am having trouble finding something pertaining to my particular problem. I apologize for a possible repeated topic, but help would be greatly appreciated.
I have two tables that contain gift card sales. I have to verify that the amounts match.
I'm looking for help writing a query that will return everything from TABLE A even if there is no matching record in TABLE B.
TABLE A
| id | business_date | am_pm | gift_cards_sold |
================================================
103 | 2011-10-06 | PM | 175.03
104 | 2011-10-06 | PM | 135.03
105 | 2011-10-06 | PM | 250.74
106 | 2011-10-06 | PM | 180.44
107 | 2011-10-06 | PM | 150.10
108 | 2011-10-06 | PM | 130.00
TABLE B
| id | business_date | am_pm | gift_cards_sold |
================================================
103 | 2011-10-06 | PM | 100.03
105 | 2011-10-06 | PM | 250.74
106 | 2011-10-06 | PM | 180.44
107 | 2011-10-06 | PM | 150.10
Here is my query so far (obviously something wrong with it)
SELECT A.id AS ID, A.gift_cards_sold AS A_SOLD, B.gift_cards_sold AS B_SOLD
FROM A
LEFT JOIN B
USING (id)
WHERE A.am_pm = 'PM'
AND A.business_date = '2011-10-06'
AND B.business_date = '2011-10-06'
GROUP BY A.id
ORDER BY A.id ASC
Here is the result:
| id | A_SOLD | B_SOLD |
========================
103 | 175.03 | 100.03
105 | 250.74 | 250.74
106 | 180.44 | 180.44
107 | 150.10 | 150.10
As you can see, there is a variance with id of 103. However, I need to have the result show every id regardless if there is a match or not. The result set is what I would expect from using an Inner Join.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需将
B.business_date = '2011-10-06'
条件从WHERE
移至ON
子句即可。当您使用LEFT
连接时,关于第二个表的列(IS (NOT) NULL
除外)的WHERE
条件实际上会取消>LEFT JOIN
,然后它充当INNER JOIN
。Simply move the
B.business_date = '2011-10-06'
condition from theWHERE
to theON
clause. When you have aLEFT
join, aWHERE
condition about the second table's columns (exceptIS (NOT) NULL
ones) actually cancels theLEFT JOIN
, it then acts as anINNER JOIN
.你应该使用这样的东西:
You should use something like this:
我不确定您为什么使用“group by”子句 - 这里不需要它,并且在大多数其他数据库中,这将是一个语法错误。
我假设您只需要匹配 id 上的行。
这应该有效:
I'm not sure why you were using a "group by" clause - it's not needed here, and in most other databases, would be a syntax error.
I'm assuming that you only need to match the rows on id.
This should work:
问题在于 B.business_date 的 where 条件,它也应该允许 null
Problem is with the where condition for B.business_date, it should also allow null