SQL笔记

  1. SELECT DISTINCT statement is used to return only distinct (different) values.
SELECT COUNT(DISTINCT Country) FROM Customers;
  1. Not equal: <>/!=
  2. UNION selects only distinct values. Use UNION ALL to also select duplicate values
  • DATE - format YYYY-MM-DD
  • DATETIME - format: YYYY-MM-DD HH:MI:SS
  • TIMESTAMP - format: YYYY-MM-DD HH:MI:SS
  • YEAR - format YYYY or YY
  1. Self join
SELECT a.column_name, b.column_name...
FROM table1 a, table1 b
WHERE a.common_field = b.common_field;

e.g. leetcode 181:
收入超过经理的员工

select a.name as Employee
from Employee a, Employee b
where a.Managerid = b.id and a.salary>b.salary
  1. Having 的用法
    having vs where
    The WHERE clause applies the condition to individual rows before the rows are summarized into groups by the GROUP BY clause. However, the HAVING clause applies the condition to the groups after the rows are grouped into groups.

e.g. leetcode 182
找出重复的email, 要用having

select email from person group by email having count(email)>1
  1. 删除表格中重复且id大的:
    leetcode 196
delete a 
from Person a, Person b 
where a.email = b. email and a.id > b.id
  1. 时间差
    mysql 函数
    datediff(被减时间,减时间)

  2. 笛卡尔积/直积(Cartesian product)
    cross join: returns the Cartesian product of the sets of records from two or more joined tables. Thus, it equates to an inner join where the join-condition always evaluates to either True or where the join-condition is absent from the statement.

  3. 整除:div
    5 div 2 = 2;
    取余:mod
    5 mod 2 = 1;
    四舍五入:round
    round(1.5) = 2;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值