zoukankan      html  css  js  c++  java
  • 常用的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
    
  • 相关阅读:
    剑指offer字符串列表
    剑指offer数组3
    剑指offer数组2
    剑指offer数组1
    剑指offer数组列表
    tensorflow+ssd_mobilenet实现目标检测的训练
    Win7+keras+tensorflow使用YOLO-v3训练自己的数据集
    Java是如何实现跨平台的
    Xshell 、PuTTY 复制文件到Linux
    Asp.Net Core2.0在linux下发布
  • 原文地址:https://www.cnblogs.com/pengdt/p/12240652.html
Copyright © 2011-2022 走看看