zoukankan      html  css  js  c++  java
  • 查询:基本查询、条件查询

    基本查询

           -- 查询所有字段

           -- select * from 表名;

           select * from students;

           select * from classes;

           select id, name from classes;

           -- 查询指定字段

           -- select 列1,列2,... from 表名;

           select name, age from students;

          

           -- 使用 as 给字段起别名

           -- select 字段 as 名字.... from 表名;

           select name as 姓名, age as 年龄 from students;

           -- select 表名.字段 .... from 表名;

           select students.name, students.age from students;

          

           -- 可以通过 as 给表起别名

           -- select 表名.字段 .... from 表名 as 别名;

           select students.name, students.age from students;

           select s.name, s.age from students as s;

           -- 失败的select students.name, students.age from students as s;

           -- 消除重复行

           -- distinct 字段

           select distinct gender from students;

    条件查询

    比较运算符

                  -- select .... from 表名 where .....

                  -- >

                  -- 查询大于18岁的信息

                  select * from students where age>18;

                  select id,name,gender from students where age>18;

                  -- 查询小于18岁的信息

                  select * from students where age<18;

                  -- 查询年龄为18岁的所有学生的名字

                  select * from students where age=18;

    逻辑运算符

                  -- and

                  -- 18到28之间的所以学生信息

                  select * from students where age>18 and age<28;

                  -- 失败select * from students where age>18 and <28;

                  -- 18岁以上的女性

                  select * from students where age>18 and gender="女";

                  select * from students where age>18 and gender=2;

                  -- or

                  -- 18以上或者身高查过180(包含)以上

                  select * from students where age>18 or height>=180;

                  -- not

                  -- 不在 18岁以上的女性 这个范围内的信息

                  -- select * from students where not age>18 and gender=2;

                  select * from students where not (age>18 and gender=2);

                  -- 年龄不是小于或者等于18 并且是女性

                  select * from students where (not age<=18) and gender=2;

    模糊查询

                  -- like

                  -- % 替换1个或者多个

                  -- _ 替换1个

                  -- 查询姓名中 以 "小" 开始的名字

                  select name from students where name="小";

                  select name from students where name like "小%";

                  -- 查询姓名中 有 "小" 所有的名字

                  select name from students where name like "%小%";

                  -- 查询有2个字的名字

                  select name from students where name like "__";

                  -- 查询有3个字的名字

                  select name from students where name like "__";

                  -- 查询至少有2个字的名字

                  select name from students where name like "__%";

                  -- rlike 正则

                  -- 查询以 周开始的姓名

                  select name from students where name rlike "^周.*";

                  -- 查询以 周开始、伦结尾的姓名

                  select name from students where name rlike "^周.*伦$";

    范围查询

                  -- in (1, 3, 8)表示在一个非连续的范围内

                  -- 查询 年龄为18、34的姓名

                  select name,age from students where age=18 or age=34;

                  select name,age from students where age=18 or age=34 or age=12;

                  select name,age from students where age in (12, 18, 34);

                 

                  -- not in 不非连续的范围之内

                  -- 年龄不是 18、34岁之间的信息

                  select name,age from students where age not in (12, 18, 34);

                  -- between ... and ...表示在一个连续的范围内

                  -- 查询 年龄在18到34之间的的信息

                  select name, age from students where age between 18 and 34;

                 

                  -- not between ... and ...表示不在一个连续的范围内

                  -- 查询 年龄不在在18到34之间的的信息

                  select * from students where age not between 18 and 34;

                  select * from students where not age between 18 and 34;

                  -- 失败的select * from students where age not (between 18 and 34);

    空判断

                  -- 判空is null

                  -- 查询身高为空的信息

                  select * from students where height is null;

                  select * from students where height is NULL;

                  select * from students where height is Null;

                  -- 判非空is not null

                  select * from students where height is not null;

  • 相关阅读:
    在线程中使用OpenFileDialog
    Log4net附加
    SQL表间列根据条件复制(赋值)
    DataGridView使用
    Latex使用总结及备忘
    Windows获取文件状态
    TabControl取消首次定位焦点
    C#跨线程调用
    电子词典的查寻程序,发送和接收应答程序
    电子词典的相关子函数db.c程序
  • 原文地址:https://www.cnblogs.com/yongfuxue/p/10037607.html
Copyright © 2011-2022 走看看