Commonly used SQL command

Data type
to four, the other need not, can be saved with a time stamp of a string

  • Digital: int
  • Long numbers: bigint
  • String: varchar (len), the most common
  • Long strings: longtext

Create a table

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;;

Number of current table , do not usecount(*)

select count(1) from t_it

Multi-table query Precautions

# 多表查询如果有一个表是没值的,会导致没有结果,因为笛卡尔积是相乘的,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

Paging query

select * from t_it ORDER BY createTime DESC limit 10,10

Find the one and the next

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

Guess you like

Origin www.cnblogs.com/pengdt/p/12240652.html