简体   繁体   English

在JOIN的ON子句中使用别名

[英]Using Aliases in the ON Clause of a JOIN

New to Stack Overflow (and coding in general). 堆栈溢出的新功能(通常是编码)。

I did some research but was unable to find an answer to the following problem: 我做了一些研究,但是找不到以下问题的答案:

How can I join two tables ON the results of functions applied to dimensions, rather than on the dimensions themselves? 如何在应用于维的函数结果上而不是维本身上联接两个表?

ie I want to join the following two tables on the lowercase results of the function lower() rather than joining on the case ambiguous dimensions as they are. 即我想在函数lower()的小写结果上联接以下两个表,而不是在大小写不明确的维度上联接。

SELECT
lower(first_name) as firstname
,lower(last_name) as lastname
,lower(email) as email1
,total_donated
From BensData.Donations As a

JOIN EACH

(Select
lower(first_name) as first
,lower(last_name) as last
,lower(email) as email2
,sum(amount) as total_donated
From BensData.Donations 
GROUP BY email2, first, last) As b

ON a.email1=b.email2 AND a.firstname=b.first AND a.lastname=b.last

It does not let me join on the aliases I create in the first table (a), however, if I join ON the original dimensions in table a (first_name and last_name) then the results are based on the case ambiguous dimensions, and give an undesired result. 它不允许我加入在第一个表(a)中创建的别名,但是,如果我加入表a中的原始维度(first_name和last_name),则结果基于大小写不明确的维度,并给出不良结果。

I hope that was clear. 我希望这很清楚。

Thanks for any help! 谢谢你的帮助!

Try using two subqueries like this: 尝试使用两个子查询,如下所示:

SELECT
a.firstname
,a.lastname
,a.email1
,a.total_donated
FROM

(SELECT
lower(first_name) as firstname
,lower(last_name) as lastname
,lower(email) as email1
,total_donated
From BensData.Donations) As a

JOIN EACH

(Select
lower(first_name) as first
,lower(last_name) as last
,lower(email) as email2
,sum(amount) as total_donated
From BensData.Donations 
GROUP BY email2, first, last) As b

ON a.email1=b.email2 AND a.firstname=b.first AND a.lastname=b.last

In your original query, a is just an alias for BensData.Donations, so you can only join on fields present in that table. 在原始查询中,a只是BensData.Donations的别名,因此您只能联接该表中存在的字段。

I have never heard of join each and it is not documented as a syntax for MySQL joins (see here). 我从未听说过join each ,也没有将其记录为MySQL连接的语法(请参见此处)。

Try this from clause: 试试这个from子句:

From BensData.Donations a JOIN
     (Select lower(first_name) as first, lower(last_name) as last,
             sum(amount) as total_donated
      From BensData.Donations 
      GROUP BY first, last
     ) b
     ON a.firstname = b.first AND a.lastname = b.last

Your version was also missing commas in the subquery between the columns. 您的版本在列之间的子查询中也缺少逗号。

For clarity, you should probably write the from clause as: 为了清楚起见,您可能应该将from子句写为:

     ON lower(a.firstname) = b.first AND lower(a.lastname) = b.last

or remove the lower() from the subquery. 或从子查询中删除lower() Otherwise, you are dependent on the default collations for the server, database, and table for what the on clause really does. 否则,您将依赖于服务器,数据库和表的默认归类来确定on子句的实际作用。

Thanks for everyone's help! 感谢大家的帮助!

Particularly sprocket who pointed me in the right direction! 特别扣人心弦的人向我指出了正确的方向! The main difference in his code and mine is that mine does not have the table aliases appended on the front of each dimension of the first SELECT clause (eg **a.**fistname, **a.**lastname, -----> firstname, lastname) 他的代码和我的代码的主要区别在于,我的代码没有在第一个SELECT子句的每个维的前面附加表别名(例如** a。** fistname,** a。** lastname,- ->名字,姓氏)

For some reason BigQuery kept giving me an error because of the table aliases. 由于某种原因,BigQuery一直由于表别名而给我一个错误。

Here's the code that worked. 这是起作用的代码。

SELECT
firstname
,lastname
,email1
,total_donated
FROM

(SELECT
lower(first_name) as firstname
,lower(last_name) as lastname
,lower(email) as email1
From BensData.Donations) As a

JOIN EACH

(Select
lower(first_name) as first
,lower(last_name) as last
,lower(email) as email2
,sum(float(amount)) as total_donated
From BensData.Donations 
GROUP BY email2, first, last) As b

ON a.email1=b.email2 AND a.firstname=b.first AND a.lastname=b.last

Thanks all for your help! 感谢你的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM