[英]MySQL Case-insensitive Join
I have a table named work that contains registration info for everyone in the enterprise, like this: 我有一个名为work的表,其中包含企业中每个人的注册信息,如下所示:
Table: work 表:工作
FirstName LastName SponsorshipStatus EnrollmentStatus AdjudicationStatus
--------- -------- ----------------- ---------------- ------------------
JANE DOE Complete Incomplete Incomplete
JOHN DOE Complete Complete Incomplete
MONTY PYTHON Complete Complete Complete
MARY POPPINS Complete Complete Complete
A department manager gives me a list of her employees like the one immediately below and she needs a status update: 部门经理给我一份她的员工名单,如下面的员工,她需要更新状态:
Table: employees 表:员工
FirstName LastName
--------- --------
John Doe
Mary Poppins
Humpty Dumpty
Knowing that the case of the two tables do not match, I try the following query: 知道两个表的情况不匹配,我尝试以下查询:
SELECT employees.FirstName, employees.LastName,
SponsorshipStatus, EnrollmentStatus, AdjudicationStatus
FROM employees LEFT JOIN work
ON (UPPER(employees.FirstName) LIKE UPPER(work.FirstName)
AND UPPER(employees.LastName) LIKE UPPER(work.LastName));
...and it produces the following: ...并产生以下结果:
Query Result: 查询结果:
FirstName LastName SponsorshipStatus EnrollmentStatus AdjudicationStatus
--------- -------- ----------------- ---------------- ------------------
JOHN DOE NULL NULL NULL
MARY POPPINS NULL NULL NULL
HUMPTY DUMPTY NULL NULL NULL
This is what I expect to get from the query: 这是我希望从查询中得到的:
FirstName LastName SponsorshipStatus EnrollmentStatus AdjudicationStatus
--------- -------- ----------------- ---------------- ------------------
JOHN DOE Complete Complete Incomplete
MARY POPPINS Complete Complete Complete
HUMPTY DUMPTY NULL NULL NULL
What am I doing wrong here? 我在这做错了什么? The left join is working correctly, but it is not doing the match and pulling in the relevant data from the work table, as evidenced by all the nulls.
左连接正常工作,但它没有进行匹配并从工作表中提取相关数据,正如所有空值所证明的那样。
I have already looked at numerous posts here, and none of them seem to clearly help me here. 我已经在这里查看过很多帖子,但这些帖子似乎都没有在这里帮助我。
You're misusing LIKE
. 你在滥用
LIKE
。 Don't use LIKE
unless you're pattern-matching. 除非您要进行模式匹配,否则不要使用
LIKE
。 Change 更改
UPPER(employees.FirstName) LIKE UPPER(work.FirstName)
to 至
UPPER(employees.FirstName) = UPPER(work.FirstName)
Do the same with the lastname, too. 对姓氏也要这样做。
Since MySQL 5.0.3, when you retrieve values for a VARCHAR
column, any trailing spaces are included. 从MySQL 5.0.3开始,当您检索
VARCHAR
列的值时,会包含任何尾随空格。 This can be a concern because when using =
comparison, trailing spaces are ignored, whereas with LIKE
trailing spaces are considered. 这可能是一个问题,因为当使用
=
比较时,忽略尾随空格,而考虑LIKE
尾随空格。
SELECT 'foo' = 'foo ';
// Returns true
SELECT 'foo' LIKE 'foo ';
// Returns false
So, be sure to use the appropriate operator. 因此,请确保使用适当的运算符。
Since the collation on the column is CASE INSENSITIVE
, you don't need the UPPER()
function. 由于该列的排序规则为
CASE INSENSITIVE
,因此您不需要UPPER()
函数。 In fact, dropping UPPER
will allow MySQL to utilize an index on the column (using a function on a column generally negates the use of an index). 实际上,删除
UPPER
将允许MySQL利用列上的索引(在列上使用函数通常会否定索引的使用)。
// With case insensitive collation
SELECT 'FOO' = 'foo';
// Returns true
// LIKE will work the same
SELECT 'FOO' LIKE 'foo';
// Returns true
Since =
and LIKE
return the same results for you, however, I don't know what the issue is. 既然
=
和LIKE
为你返回相同的结果,我不知道是什么问题。
You should also specify the table name for all columns in a join to avoid any chance of ambiguous columns (if you were to add similar columns to the other table in the future), and for readability: 您还应该为连接中的所有列指定表名,以避免出现模糊列的可能性(如果您将来要向其他表添加类似的列),并且为了可读性:
SELECT employees.FirstName, employees.LastName,
work.SponsorshipStatus, work.EnrollmentStatus, work.AdjudicationStatus
FROM employees
LEFT JOIN work
ON employees.FirstName = work.FirstName
AND employees.LastName = work.LastName;
However, if the columns were ambiguous, you should get an error. 但是,如果列不明确,则应该得到一个错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.