[LeetCode]Mysql小本本

常用方法

  • 累加型题目,可以考虑使用笛卡尔积进行自表连接,连接后的表进行where条件进行筛选、group by分组操作。
  • union:需要把两列作一列可以用union,union的两张表查询的字段不一定要名称相同。
    例题:[Leetcode]1264. 页面推荐 https://leetcode-cn.com/problems/page-recommendations/

注意点

  • 两张表都有的字段,在查询和where里都要指明对应的表。
  • 函数:sqrt() pow() round()
  • limit不能和in一起使用,需要在原来基础上多加一层查询。这里使用=。
    例574. 当选者
# Write your MySQL query statement below
select Name
from Candidate
where id =(-- limit不能和in一起使用,需要在原来基础上多加一层查询
    select candidateid
    from Vote
    group by candidateid
    order by count(CandidateId) desc limit 1 
)

posted on 2020-05-23 23:23  coding_gaga  阅读(141)  评论(0编辑  收藏  举报

导航