[英]What T-SQL query would retrieve all values of the Email column where values of First_Name and Last_ Name columns are in the Email column?
There's a table with 3 columns, 'First_Name', 'Last_Name', 'Email'.有一个包含 3 列的表,“First_Name”、“Last_Name”、“Email”。 Some values in Email column contain First_Name and Last_Name column values concatenated together.
Email 列中的某些值包含串联在一起的 First_Name 和 Last_Name 列值。 Ie, Email = 'JohnSmith22@example.com' where First_Name = 'John', Last_Name = 'Smith'.
即,电子邮件 = 'JohnSmith22@example.com',其中 First_Name = 'John',Last_Name = 'Smith'。
Other Email values for some rows are unrelated.某些行的其他电子邮件值不相关。 Ie First_Name = 'Steven', Last_Name = 'Paul', Email = 'random12345@example2.com'.
即名字 = 'Steven',姓氏 = 'Paul',电子邮件 = 'random12345@example2.com'。 What query would retrieve only values of Email column, where First_Name and Last_Name are in the column?
什么查询只会检索 Email 列的值,其中 First_Name 和 Last_Name 在列中?
You can check if the email
starts with the concatenation of the first and last names, using like
:您可以使用
like
来检查email
是否以名字和姓氏的串联开头:
select t.*
from mytable t
where email like first_name + last_name + '%'
In case of the name field have a heterogeneous character case , you need to convert it to lower and check.如果 name 字段具有异类字符大小写,则需要将其转换为lower 并检查。 The other thing is sometimes the order of First and Last name in the email might be different so you need to consider this case as well.
另一件事是有时电子邮件中名字和姓氏的顺序可能不同,因此您也需要考虑这种情况。
SELECT *
FROM theTable
WHERE lower(email) LIKE '%' + lower(name) + '%' + LOWER(lastname) + '%'
OR lower(email) LIKE '%' + lower(lastname) + '%' + LOWER(name) + '%'
What query would retrieve only values of Email column, where First_Name and Last_Name are in the column?
什么查询只会检索 Email 列的值,其中 First_Name 和 Last_Name 在列中?
If you want first and last name in the column -- but other characters as well -- I would suggest:如果您想要列中的名字和姓氏 - 但也需要其他字符 - 我建议:
where email like '%' + first_name + '%@%' and
email like '%' + last_name + '%@%'
This checks that the first and last names are before the @
, in any order -- and with any other characters.这会检查名字和姓氏是否在
@
之前,以任何顺序 - 以及任何其他字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.