lxl-php第六天

数据库


字段属性

  • 字符串

         char 定长
         varchar 不定长,默认值为255
    
  • 数值

         int 整数
         float 浮点数,对精确度要求低的
         decimal 浮点数,对精确度要求高的,适用于货币之类的
    
  • text

         用于较大的文本
    
  • 日期相关

          datetime   年月日 时分秒
          date 年月日
          time 时分秒
    

字段约束

     null \ not null   限定是否为空
     default 设置默认值,若是字符串,应加引号
     primary key 主键 一张表只能一个,一般用于id,自动生成
     auto_increment 自增长
     unique key 这个字段不能重复 值是唯一的
     comment 注释

查询语句

    1. select * from mytable -->查询mytable中所有的数据
    2. select id,name,sex from mytable --> 查询mytable中的 id,name,sex
    3. select * from mytable where id >= 20 --> 带条件查询,从mytable中查询 id>=20的所有数据
    4. select * from mytable where id<30 and sex ='女' --> 多条件查询,从mytable中查询id<30 并且 sex是'女'的所有数据

增加语句

    insert into mytable values(null, 值1,值2...)

删除语句

    delete from mytable where id= 20;-->在mytable中 删除id=20的数据

修改语句

    update mytable set name='鲁小小',age=44  where id= 5; -->把mytable中的id=5 的数据修改 name = '鲁小小' age=44

函数

  • count() 统计

      select count(*) from mytable;--> 统计mytable中的条数,无法对null值进行计算
    
  • min()/max()

      select min(age) from mytable;
      select max(name) from mytable;//遇到字符,是根据字符的编码值来比较大小的
    
  • avg() 平均值

      如果avg() 括号里是字符串,则返回0
    

排序

  • 升序 select * form 名 order by 值 asc

      select * from mytable order by id desc --> 在mytable中根据id 降序排序
    
  • 降序 select * form 名 order by 值 desc

      select * from mytable order by id asc --> 在mytable中根据id 升序排序
      select * from mytable order by id asc,age asc --> 在mytable中根据id 升序 ,age升序排序
    

复杂查询

  • limit :获取制定范围的数据

      limit n; --> 获取前n条
      limit n,m; --> n是偏移,从0开始;m是数量
      eg: limit 2,4; --> 从第三行开始,取4行
      分页的公式:
              select * from mytable limit ($nowpage - 1)*$pagesize, $pagesize;
    

多表查询

  • 内联查询

      select * from A join B on A.cid = B.id;
    
      select * from A inner join B on A.cid = B.id;
    
  • 左联结

      select * from A left join B on A.cid = B.id;
    
  • 右联结

      select * from A right join B on A.cid = B.id;
    
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 50个常用的sql语句Student(S#,Sname,Sage,Ssex) 学生表Course(C#,Cname...
    哈哈海阅读 1,260评论 0 7
  • DataBase-MySQL-PGSQL rm -f *.backup pg_dump -h localhost ...
    燕京博士阅读 446评论 0 0
  • 很久没有半夜醒来,回想过去的688天,永远不会忘记王静妹妹的话:完颜古方你可以试试,就是这么简单的一句话,让我从此...
    美需要勇气阅读 296评论 0 0
  • 楼下不远处小卖部的店主是个温柔、善解人意的小妇人,她盈盈的浅笑、柔顺不做作的语调给人如沐春风的感觉,所以总是喜欢...
    彤乐阅读 468评论 0 0