前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >「SQL面试题库」 No_119 Find Users With Valid E-Mails

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

作者头像
不吃西红柿
发布2023-10-16 10:49:41
2420
发布2023-10-16 10:49:41
举报
文章被收录于专栏:信息技术智库信息技术智库

今日真题

题目介绍: 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"的写法更简洁。

  • 已经有灵感了?在评论区写下你的思路吧!
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-10-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 今日真题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档