![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
SQL
剑雨星澄
这个作者很懒,什么都没留下…
展开
-
LeetCode.626. 换座位
思路:检索每一个id值,如果为奇数则设为i+1,如果是偶数则设为i-1,注意末位是奇是偶,重新输出即可。代码:select if(id<(select count(*) from seat),if(id%2=0,id-1,id+1),if(id%2=0,id-1,id)) as id,studentfrom seatorder by id;...原创 2018-10-28 11:45:54 · 207 阅读 · 0 评论 -
LeetCode.182. 查找重复的电子邮箱
编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。示例:±—±------------+| Id | Email |±—±------------+| 1 | a@b.com || 2 | c@d.com || 3 | a@b.com |±—±------------+根据以上输入,你的查询应返回以下结果:±-------------+| Em...原创 2018-10-24 19:12:01 · 313 阅读 · 0 评论 -
LeetCode.620. 有趣的电影
思路:组合条件,降序排列。代码:SELECT *FROM cinemaHAVING description != 'boring' AND id % 2 = 1ORDER BY rating DESC;原创 2018-10-26 14:06:01 · 229 阅读 · 0 评论 -
LeetCode.595. 大的国家
代码:SELECT name, population, areaFROM WorldWHERE population > 25000000 OR area > 3000000;原创 2018-10-26 13:46:18 · 251 阅读 · 0 评论 -
LeetCode.197. 上升的温度
思路:依然是创建两个对象,进行比较。需要用到to_day函数,可将日期线性转化为整数值,两个日期的差值可以相减得到。代码:SELECT w1.IdFROM Weather w1, Weather w2WHERE w1.Temperature > w2.Temperature AND TO_DAYS(w1.RecordDate)...原创 2018-10-26 13:40:21 · 205 阅读 · 0 评论 -
LeetCode.181. 超过经理收入的员工
思路:此题题意不是很明确。正确理解方式是,一般员工后面的ManagerId是主管此员工的经理的Id,经理的ManagerId为空值。创建两个实例e1/e2,其中e1用于查普通员工,e2用于查经理。注意“连接”字段。SELECT e1.name AS EmployeeFROM Employee e1, Employee e2WHERE e1.Manag...原创 2018-10-26 13:30:28 · 167 阅读 · 0 评论 -
LeetCode.175. 组合两个表
思路:左连接Person表即可。代码:SELECT FirstName, LastName, City, StateFROM Person LEFT JOIN Address ON Person.PersonId = Address.PersonId;原创 2018-10-26 13:05:45 · 171 阅读 · 0 评论 -
LeetCode.627. 交换工资
思路:条件控制case…when…then…else…,逐一扫描update。代码:UPDATE salary SET sex = CASE sex WHEN 'm' THEN 'f' ELSE 'm' END;原创 2018-10-26 12:49:40 · 161 阅读 · 0 评论 -
LeetCode.596. 超过5名学生的课
思路1:即按课程分组,由于有重复数据,按学生计数时需要使用distinct.SELECT CLASS classFROM COURSESGROUP BY CLASSHAVING COUNT(DISTINCT STUDENT) >= 5分析1:此代码是我最初的思路,也是LeetCode记录的最优解。但是在最近增加了一个极大的测试用例导致超时。思路2:二次选择。是第一...原创 2018-10-26 12:42:23 · 204 阅读 · 0 评论 -
LeetCode.176. 第二高的薪水
思路1:即降序排序后取第二个。这里难搞的是空值情况。最后使用isnull函数,如果选择为空,则返回为空。代码1:SELECT ifnull( ( SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT 1 OFFSET 1 ), NULL ) AS SecondHighestSalary;思路2:第二高薪...原创 2018-10-26 12:36:33 · 232 阅读 · 0 评论 -
LeetCode.196. 删除重复的电子邮箱
思路1:创建实例p1/p2,比较两者中的Email值。代码1:delete p1from Person p1,Person p2where p1.Email=p2.Email and p1.Id>p2.Id;思路2:多重选择,需要注意每次选择必有别名。delete from Personwhere Id not in (select Id ...原创 2018-10-26 12:28:42 · 291 阅读 · 0 评论 -
LeetCode.183. 从不订购的客户
思路1:左连接,连接键为Customers的ID字段和Orders的CustomerId字段。连接完后,CustomerId中空值即为所求客户。代码:select c.name as Customersfrom Customers cleft join Orders o on o.CustomerId=c.Idwhere o.CustomerId is null;思路2:...原创 2018-10-26 12:19:32 · 144 阅读 · 0 评论 -
LeetCode.180. 连续出现的数字
分析:自连接两次,每次差值为1,检索第一个表即可。代码:select distinct l1.Num as ConsecutiveNumsfrom Logs l1 left join Logs l2 on l1.Id=l2.Id-1 left join Logs l3 on l2.Id=l3.Id-1where l1.Num=L2.Num and l2.Num=l3.Num;...原创 2018-10-28 11:51:46 · 140 阅读 · 0 评论 -
LeetCode.178. 分数排名
思路:即无间隔排名。对于每个数字比较分数中有多少个大于等于自己的数,即为自身排名。代码:select Score,(select count(distinct Score) from Scores as s2 where s2.Score >= s1.Score) as Rank from Scores as s1order by Score DESC;...原创 2018-10-28 11:49:24 · 176 阅读 · 0 评论 -
LeetCode.184. 部门工资最高的员工
思路:首先先对返回表头命名,之后连接两表并设置连接键。接下来不可使用group by语句分组,可能是连表导致的问题将导致最后的结果中姓名不匹配。因此使用二次子查询。代码:select d.name as Department,e.name as Employee,e.Salary as Salaryfrom Employee e join Department don e.Depa...原创 2018-10-27 19:33:15 · 269 阅读 · 0 评论