常用的SQL命令
数据类型
就四个,其他用不着,时间可以用字符串时间戳的形式存
- 数字: int
- 长数字: bigint
- 字符串: varchar(len),最常用
- 长字符串:longtext
创建表
create table student(
id int auto_increment primary key, 自增长&&主键
name varchar(20) not null, 非空约束
age int not null, 非空约束
gender bit default 1, 默认约束
address varchar(20) unique, 唯一约束
isDelete bit default 0 默认约束
) CHARSET=utf8;;
当前表的条数,别用count(*)
select count(1) from t_it
多表查询注意点
# 多表查询如果有一个表是没值的,会导致没有结果,因为笛卡尔积是相乘的,0*100也是0
select
A.*,B.*,C.*
from
t_a A,t_b B,t_c C
# 所以需要要外连接
select
A.*,B.*,C.*
from
t_a A
left join
t_b B
on A.id == B.id
left join
t_c C
on B.id == C.id
分页查询
select * from t_it ORDER BY createTime DESC limit 10,10
查找上一篇和下一篇
select A.*,B.id 'nextId',C.id 'lastId'
from
(select * from t_mine where id='001') A
left join
(select id from t_mine where createTime > (select createTime from t_mine where id='001') ORDER BY createTime LIMIT 1) B
on A.id!=B.id
left join
(select id from t_mine where createTime < (select createTime from t_mine where id='001') ORDER BY createTime desc LIMIT 1) C
on A.id!=C.id