MySQL数据库-leetcode/牛客-面试题
MySQL数据库命令
MySQL面试知识点干货
Leetcode题库
一、简单题
1、查找重复的电子邮箱
编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。
示例:
Id | |
---|---|
1 | a@b.com |
2 | c@d.com |
3 | a@b.com |
根据以上输入,你的查询应返回以下结果:
a@b.com |
说明:所有电子邮箱都是小写字母。
解题思路:group by … having
知识点:https://blog.csdn.net/m0_37882192/article/details/109827572
// 解法1
select email from person group by email having count(email)>1
//解法2
select email from (select count(1) as t,email from person group by email)r where r.t>1;
//解法3
select distinct(p1.Email) from Person p1 join Person p2 on p1.Email = p2.Email AND p1.Id!=p2.Id
2、变更性别
给定一个 salary 表,如下所示,有 m = 男性 和 f = 女性 的值。交换所有的 f 和 m 值(例如,将所有 f 值更改为 m,反之亦然)。要求只使用一个更新(Update)语句,并且没有中间的临时表。
注意,您必只能写一个 Update 语句,请不要编写任何 Select 语句。
例如:
id | name | sex | salary |
---|---|---|---|
1 | A | m | 2500 |
2 | B | f | 1500 |