SQL - 在两个表的@符号之前匹配电子邮件地址文本

时间:2014-01-30 22:57:43

标签: mysql sql ms-access

我正在尝试匹配两个表中的类似电子邮件地址。我想要做的是找到表1中的电子邮件地址类似于表2中的电子邮件地址的记录(其中文本在@符号之前是相同的)。例如,JohnSmith @ gmail.com将与JohnSmith@yahoo.com匹配。

4 个答案:

答案 0 :(得分:0)

内部联接表AB,其中包含 theta (使用T-SQL语法):

ON SUBSTRING(A.Email, 1, CHARINDEX('@', A.Email)) = SUBSTRING(B.Email, 1, CHARINDEX('@', B.Email))

当从MS Access切换到MySQL时,您的语法将彻底改变,因此只需使用MySQL语法。以上是你问题的工作解决方案,但它会引导你走向它。

答案 1 :(得分:0)

以下是执行此操作的MySQL语法:

on substring_index(a.Email, '@', 1) = substring_index(b.Email, '@', 1)

答案 2 :(得分:0)

在SQl中你可以做一些像这样的事情并在@之前选择所有内容,两个表中的列名是'email':

SELECT SUBSTRING(Email,0, CHARINDEX('@',Email)) 
from [TABLE1] 
where SUBSTRING(email,0, CHARINDEX('@',email))
in (SELECT SUBSTRING(email,0, CHARINDEX('@',email)) from [TABLE2])

答案 3 :(得分:0)

对于Access,您可以使用如下查询:

SELECT
    t1.ID AS ID1,
    t1.Email AS Email1,
    t2.ID AS ID2,
    t2.Email AS Email2
FROM
    Table1 AS t1
    INNER JOIN
    Table2 as t2
        ON Left(t1.Email,InStr(t1.Email,'@')-1) = Left(t2.Email,InStr(t2.Email,'@')-1)

编辑重新评论

如果由于您尝试加入的字段中的Null值而收到“条件表达式中的数据类型不匹配”错误,请尝试使用

SELECT 
    t1.ID AS ID1, 
    t1.Username AS Email1, 
    t2.ID AS ID2, 
    t2.email AS Email2
FROM 
    USERS AS t1 
    INNER JOIN 
    LICENSE AS t2 
        ON Left(Nz(t1.Username,''),InStr(Nz(t1.Username,''),'@')-1) = Left(Nz(t2.email,''),InStr(Nz(t2.email,''),'@')-1);

修改2

...或者你可能更喜欢这样的东西:

SELECT 
    t1.ID,
    t1.Username AS Email
FROM 
    USERS AS t1 
WHERE
    EXISTS
        (
            SELECT * 
            FROM LICENSE AS t2 
                WHERE Left(Nz(t1.Username,''),InStr(Nz(t1.Username,''),'@')-1) = Left(Nz(t2.email,''),InStr(Nz(t2.email,''),'@')-1)
        )