zoukankan      html  css  js  c++  java
  • sql总结-----基本查询

    创建一个测试表,插入一些测试数据:

    Create Table Cstable (Id Int,Age Int,Name Varchar2(8),Address Varchar2(20));
    Insert Into Cstable Values(1,13,'张三','深圳');
    Insert Into Cstable Values(2,23,'李四','上海');
    Insert Into Cstable Values(3,23,'王五','北京');
    Insert Into Cstable Values(4,43,'刘心','北京');
    Insert Into Cstable Values(5,11,'刘开心','北京');

    基本查询

    1、基本查询

    从表中查询某些列的值,这是最基本的查询语句。

    #语法
    SELECT 列名1,列名2 FROM 表名
    
    #举例
    select name,address from cstable;

    2、条件

    作用:按照一定的条件查询数据

    运算符:

    #语法:
    SELECT 列名1,列名2 FROM 表名 WHERE 列名1 运算符  值
    #举例
    select * from cstable where name='张三';
    select * from cstable where id >=2;
    select * from cstable where id between 2 and 3; 

    比较操作符都比较简单,不再赘述。关于LIKE,专门拿出来重点说下

    3、LIKE

    作用:模糊查询。LIKE关键字与通配符一起使用

    主要的通配符:

    #举例
    #查询所有姓张的
    select * from cstable where name like '张%'; 
    #查询最后一个字为三的
    select * from cstable where name like '%三'; 
    #查询名字包含开字的
    select * from cstable where name like '%开%'; 
    #查询名字两个字,并且姓张
    select * from cstable where name like '张_';
    

    4、AND

    AND 在 WHERE 子语句中把两个或多个条件结合起来。表示和的意思,多个条件都成立。

    #查询id大于等于2,且姓刘的
    select * from cstable where id >=2 and name like '刘%';
    

    5、OR

     OR可在 WHERE 子语句中把两个或多个条件结合起来。或关系,表示多个条件,只有一个符合即可。

    #查询姓张或者姓刘的
    select * from cstable where name like '张%' or name like '刘%';

    6、IN

    IN 操作符允许我们在 WHERE 子句中规定多个值。表示:在哪些值当中。

    #查询ID为2和4的
    select * from cstable where id in (2,4);
    

    7、NOT否定

    NOT对于条件的否定,取非。

    #查询所有不姓张的
    select * from cstable where name not like '张%';

    8、ORDER BY(排序)

    功能:对需要查询后的结果集进行排序

    #查询学生年龄,地址,并根据年龄排序
    select age,name,address from cstable order by age;
    #或者asc,默认就是升序
    select age,name,address from cstable order by age asc;
    #或者adsc,降序
    select age,name,address from cstable order by age adsc;
    

    9、MAX/MIN/SUM/AVG

    MAX 函数返回一列中的最大值。NULL 值不包括在计算中。

    MIN 函数返回一列中的最小值。NULL 值不包括在计算中。

    SUM函数查询某列的合计值。

    AVG 函数返回数值列的平均值

    select max(age) from cstable;
    select min(age) from cstable;
    select sum(age) from cstable;
    select avg(age) from cstable;
    

    10、COUNT函数

    COUNT() 函数返回匹配指定条件的行数。

    #查询有多少姓刘的学生
    select count(id) from cstable where name like '刘%';
    

     

      

  • 相关阅读:
    微信jssdk
    php读取大文件
    PHP工程师突破
    TCP/IP
    基于scrapy-redis的分布式爬虫
    pymongodb的使用和一个腾讯招聘爬取的案例
    中间件使用之(UA,IP,selenium)的使用
    windos下redis服务的后台启动
    mongodb的初步使用
    windos下安装mongodb
  • 原文地址:https://www.cnblogs.com/jinyuanliu/p/10428016.html
Copyright © 2011-2022 走看看