MySQL 几个小知识点
1.计算日期的加减
SELECT
t2.create_time,
t2.create_time + INTERVAL 5 DAY
FROM
table t2
LIMIT 100
输出结果:
2.求两个日期(指存储在不同行之间的天数)之间相差的天数,如在招聘表中确定招聘某个员工的日期与招聘下个员工的日期
SELECT
t.id,
t.create_time,
(
SELECT
min(t2.create_time)
FROM
table t2
WHERE
t2.create_time > t.create_time
)
FROM
table t
WHERE
t.id = 3
3.从表中随机返回n条记录
SELECT
intro
FROM
table
order by
rand()
limit 5
4.将空值转换为实际值
SELECT
coalesce(intro,0)
FROM
table
5.计算累加
SELECT
t2.id,t2.duration,
(SELECT
sum(t.duration)
FROM
table t
WHERE
t.id <= t2.id)
from table t2
6.计算累乘
SELECT
t2.id,
t2.duration,
(
SELECT
exp(sum(LN(t.duration)))
FROM
table t
WHERE
t.id <= t2.id
)
FROM
table t2
where t2.id <100