首页 > 数据库技术 > 详细

常用的SQL命令

时间:2020-01-29 17:15:40      阅读:105      评论:0      收藏:0      [点我收藏+]

数据类型
就四个,其他用不着,时间可以用字符串时间戳的形式存

  • 数字: 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

常用的SQL命令

原文:https://www.cnblogs.com/pengdt/p/12240652.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!