MySQL 使用 WHERE 子句进行连接
我有 2 个查询显示 2 个不同的值,但其中一个查询的结果是意外的。
查询1:
SELECT SUM(T.amount_reward_user) AS total_grouping
FROM fem_user_cards UC
LEFT JOIN fem_transactions T USING(card_number)
LEFT JOIN fem_company_login FCL
ON T.fem_company_login_id=FCL.fem_company_login_id
WHERE UC.fem_user_login_id=193
AND FCL.grouping<>0 AND T.from_group=1 AND T.authorised=1
GROUP BY UC.fem_user_login_id
上面的查询给出了用户193的总奖励金额。这里的条件是我从这个查询中得到的奖励金额必须来自从T.from_group=1可以明显看出的组。所以这似乎工作正常。
查询2:
SELECT SUM(T.amount_reward_user) AS total_grouping
FROM fem_transactions T
LEFT JOIN fem_company_login FCL
ON T.fem_company_login_id=FCL.fem_company_login_id
WHERE T.fem_user_login_id=193
AND FCL.grouping<>0 AND T.from_group=1 AND T.authorised=1
在此查询中,即使 T.from_group=1,它也会将 T.from_group=0 的奖励金额相加。有谁知道问题是什么?
例如:
用户 193 以 5 美元在 A 公司(在群组内,因此 T.from_group=1)购买了某件商品并获得了 1 美元的奖励,并以 5 美元从公司 B(在群组内,因此 T.from_group=0)购买了另一件产品)并再次获得 1 美元奖励。
我的预期输出是: 1
我得到:
Query1: 1
Query2: 2
我可以简单地使用查询 1,但是当我使用它时,我遇到了其他问题,所以我需要了解到底发生了什么!
我的目标是从数据库中获取奖励金额的总和。总和应该是不同集团公司的总和。也就是说,如果我们有 5 个不同的集团公司,并且用户在这 5 个公司中使用他的钱卡,我应该得到 5 个不同的总数。如果他在集团之外的任何公司使用他的卡,则总数不应包括来自该非集团公司的交易。我有 C++ 代码来在事务发生时向数据库提供数据。我的fem_transactions表有company_id,amount_reward_user,fem_user_login_id,from_group(这决定用户是否在组外或从组内进行交易),授权等。
我想分别过滤用户193在每个组中花费的金额。
我现在理解了 JOINTS 概念,但我需要知道为什么第二个查询上的 t.from_group 被忽略?
I have 2 queries which display 2 different values but the results from one query are unexpected.
Query1:
SELECT SUM(T.amount_reward_user) AS total_grouping
FROM fem_user_cards UC
LEFT JOIN fem_transactions T USING(card_number)
LEFT JOIN fem_company_login FCL
ON T.fem_company_login_id=FCL.fem_company_login_id
WHERE UC.fem_user_login_id=193
AND FCL.grouping<>0 AND T.from_group=1 AND T.authorised=1
GROUP BY UC.fem_user_login_id
The above query gives me the total reward amount for the user 193. Here the condition is the reward amount which i am getting out of this query must be from the group which is evident from T.from_group=1. So this seems to be working correct.
Query2:
SELECT SUM(T.amount_reward_user) AS total_grouping
FROM fem_transactions T
LEFT JOIN fem_company_login FCL
ON T.fem_company_login_id=FCL.fem_company_login_id
WHERE T.fem_user_login_id=193
AND FCL.grouping<>0 AND T.from_group=1 AND T.authorised=1
In this query even though T.from_group=1 it is adding up the reward amount from T.from_group=0 as well. Does anyone know what the problem is?
For example:
The user 193 purchased something in a company A(in the group so T.from_group=1) for $5 and got the reward of $1 and bought another product for $5 from company B(out of group so T.from_group=0) and get a reward of $1 again.
My expected output is: 1
I am getting:
Query1: 1
Query2: 2
I could simply use the query 1 but i have issues with other things when i use that so i need to understand what is going on really!!
My aim is to get the sum of reward amounts from the database. The sum should be of different grouped companies. That is if we have 5 different grouped companies and user is using his moneycard in those 5 companies, I should get 5 different totals. If he used his card in any company which is not in the group the total should not include transaction from this un-grouped company. I have C++ code to feed the database as transactions happens. My fem_transactions table has company_id, amount_reward_user, fem_user_login_id, from_group (this decides if the user has transacted out of the group or from the group), authorized etc..
I want to filter the amount spent by the user 193 in each group separately.
I understood the JOINTS concepts now but i need to know why t.from_group on the second query is ignored ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您在Where 子句中针对Left Join 右侧的列设置了条件。在这两个查询中,这是
And FCL.grouping <> 0 。这实际上将左连接更改为内连接,因为您要求 FCL.grouping 值存在且不为零(或 Null)。我认为,您想要的是将该条件移至 On 子句中:
第一个查询:
第二个查询
所有这些中尚不清楚的是您想要实现的目标。即,如果您不过滤它们,为什么还要在任一查询中使用左连接呢?如果您确实想要筛选包含
fem_company_login
值和分组值 <> 的行, 0,然后使用原始查询并将左联接更改为内联接。如果您想过滤具有分组值 <> 的人员0 或根本没有值,然后执行以下操作:The problem is that you have criteria in the Where clause against columns on the right side of the Left Join. In the both queries, this is
And FCL.grouping <> 0
. This effectively changes the Left Join to an Inner Join since you are in requiring that an FCL.grouping value exist and not be zero (or Null). What you want, I think, is to move that criteria into the On clause:First query:
Second query
What is not clear in all of this is what you are trying to achieve. I.e., why even have the Left Joins in either query if you are not filtering on them. If you do specifically want to filter for rows where there is a
fem_company_login
value with a grouping value <> 0, then use your original query and change the Left Join to an Inner Join. If you want to filter for people that either have a grouping value <> 0 or no value at all then do something like: