「SQL面试题库」 No_119 Find Users With Valid E-Mails

2023-10-16 10:49:41 浏览数 (1)

今日真题

题目介绍: Find Users With Valid E-Mails find-users-with-valid-e-mails

难度简单

SQL架构

Table:

代码语言:javascript复制
Users
代码语言:javascript复制
 --------------- --------- 
| Column Name   | Type    |
 --------------- --------- 
| user_id       | int     |
| name          | varchar |
| mail          | varchar |
 --------------- --------- 
user_id is the primary key for this table.
This table contains information of the users signed up in a website. Some e-mails are invalid.

Write an SQL query to find the users who have valid emails.

A valid e-mail has a prefix name and a domain where:

The prefix name is a string that may contain letters (upper or lower case), digits, underscore

代码语言:javascript复制
'_'

, period

代码语言:javascript复制
'.'

and/or dash

代码语言:javascript复制
'-'

. The prefix name must start with a letter.

The domain is

代码语言:javascript复制
'@leetcode.com'

.

Return the result table in any order.

The query result format is in the following example.

``` Users --------- ----------- ------------------------- | user_id | name | mail | --------- ----------- ------------------------- | 1 | Winston | winston@leetcode.com | | 2 | Jonathan | jonathanisgreat | | 3 | Annabelle | bella-@leetcode.com | | 4 | Sally | sally.come@leetcode.com | | 5 | Marwan | quarz#2020@leetcode.com | | 6 | David | david69@gmail.com | | 7 | Shapiro | .shapo@leetcode.com | --------- ----------- -------------------------

Result table: --------- ----------- ------------------------- | user_id | name | mail | --------- ----------- ------------------------- | 1 | Winston | winston@leetcode.com | | 3 | Annabelle | bella-@leetcode.com | | 4 | Sally | sally.come@leetcode.com | --------- ----------- ------------------------- The mail of user 2 doesn't have a domain. The mail of user 5 has # sign which is not allowed. The mail of user 6 doesn't have leetcode domain. The mail of user 7 starts with a period. ```

考察正则表达式的使用

代码语言:javascript复制
sql
SELECT *
FROM Users
WHERE mail REGEXP '^[a-zA-Z] [\w_\.\-]*@leetcode.com$'   
ORDER BY user_id;
代码语言:javascript复制
sql
select * from Users
where mail regexp '^[a-zA-Z] [a-zA-Z0-9_\./\-]{0,}@leetcode.com$'
order by user_id

坑点: 1、前缀可能是一个字母,比如“J@leetcode.com”,所以匹配非首字母外的前缀字符数量要用{0,}或*,不能用 。 2、题意要求:underscore '', period '.' and/or dash '-',/没加单引号,不留神可能写漏/。 3、后缀可能是“@leetcodeecom”,所以要对“.”加转义符号。 4、后缀可能是“@LEETCODE.COM”,默认是不区分大小写匹配,所以要加上“BINARY”区分大小写。 语法: 1、https://www.cnblogs.com/timssd/p/5882742.html 2、https://www.cnblogs.com/zhaopanpan/p/10133224.html 3、"双反斜杠 w"表示字母、数字、下划线,相对"a-zA-Z0-9"的写法更简洁。

  • 已经有灵感了?在评论区写下你的思路吧!

0 人点赞