如何从电子邮件中选择@domain并使用mysql从另一个表中进行匹配

如何从电子邮件中选择@domain并使用mysql从另一个表中进行匹配,mysql,sql,regex,Mysql,Sql,Regex,我有两个表,第一个是用户表 +------------------------------------ | Useid | user email | name | |-----------|----------------|----- | | 1 | sara@yahoo.com |sara | +------------------------------------ 第二是基础表 +------------------------------- |f

我有两个表,第一个是用户表

+------------------------------------
|  Useid    |  user email    | name  |
|-----------|----------------|-----  |
|  1        | sara@yahoo.com |sara   |
+------------------------------------

第二是基础表

+-------------------------------
|fo_name           |  fo_email  |  
|----------------  |------------|
|Florida universty | @yahoo.com | 
---------------------------------
注意:首先我将按用户ID搜索。它不起作用。有人能帮我吗

select fo_name,fo_email
(select 
  d_users.user_id,
 SUBSTRING_INDEX(d_users.user_email,@) // some regex or substr code to cut domain
FROM d_users
where d_users.user_id = '1'
) as substr_email
from foundation
where fo_email = substr_email;

我想选择匹配用户邮件的基础怎么做< /P> 通过正则表达式或substr_索引或任何东西

像这样的用户电子邮件sara@yahoo.com或adam@hotmail.com

基金会电子邮件,比如@yahoo.com或@hotmail.com,首先: 如果你有一个基础表,你的用户表应该有一个外键来指向这个表。 例如,用户表可以变成:

id
username
fk_foundation
name
因此,您的示例数据变成:

id = 1
username = sara
fk_foundation = id (of foundation table, in your example is not specified)
name = sara
使用当前表结构的查询可能会出错,但性能可能会很差:

select *
from user u
join foundation f
on u.email like '%' + f.fo_email
where f.fo_email = '@yahoo.com'
如果更改表结构,查询将变为:

select *
from user u
join foundation f
on u.fk_foundation = f.id
where f.fo_email = '@yahoo.com'
比第一个更快首先: 如果你有一个基础表,你的用户表应该有一个外键来指向这个表。 例如,用户表可以变成:

id
username
fk_foundation
name
因此,您的示例数据变成:

id = 1
username = sara
fk_foundation = id (of foundation table, in your example is not specified)
name = sara
使用当前表结构的查询可能会出错,但性能可能会很差:

select *
from user u
join foundation f
on u.email like '%' + f.fo_email
where f.fo_email = '@yahoo.com'
如果更改表结构,查询将变为:

select *
from user u
join foundation f
on u.fk_foundation = f.id
where f.fo_email = '@yahoo.com'

比第一次更快

我想我了解你想要什么:

select 
    f.fo_name, f.fo_email, d.user_id, d.substr_email
from
    foundation f
        inner join
    (select 
        d_users.user_id,
        RIGHT(user_email, (LENGTH(user_email) - LOCATE('@', user_email) + 1)) as substr_email
    FROM
        d_users
    where
        d_users.user_id = '1') d ON f.fo_email = d.substr_email;
虽然这是一种非常奇怪的连接表的糟糕方法,每次都要计算外键

更新:


好的,我已经更新了我的答案并在SQLFiddle上做了示例,所以请查看-

我想我了解您想要什么:

select 
    f.fo_name, f.fo_email, d.user_id, d.substr_email
from
    foundation f
        inner join
    (select 
        d_users.user_id,
        RIGHT(user_email, (LENGTH(user_email) - LOCATE('@', user_email) + 1)) as substr_email
    FROM
        d_users
    where
        d_users.user_id = '1') d ON f.fo_email = d.substr_email;
虽然这是一种非常奇怪的连接表的糟糕方法,每次都要计算外键

更新:


好的,我已经更新了我的答案并在SQLFIDLE上做了示例,所以请查看-

您给出的示例非常不清楚您的意思是“%@yahoo.com”吗您给出的示例非常不清楚您的意思是“%@yahoo.com”1241-操作数应该包含1列,子查询不能返回超过1列。。。我现在已经删除了它,所以应该可以了。1054-where子句中的未知列“substr_email”好的,我已经更新了我的答案并在SQLFIDLE上做了示例,所以请检查-1241-操作数应该包含1列,子查询不能返回超过1列。。。我现在已经删除了它,所以这应该可以用了。1054-where子句中的未知列“substr_email”好的,我已经更新了我的答案并在SQLFIDLE上做了示例,所以请检查一下-@user1080247:是的,但是你能添加它吗?@user1080247:好的,那么你可以使用第一个查询like@user1080247:是的,但是你能添加它吗?@user1080247:好的,因此,您可以将第一个查询与like一起使用