数据库学习笔记_查询(2)

查询:

1:查询的基本语法:

Select *from 表名

-from关键字后面写表名,表示数据来自于这个表,

-select后面写表中的列名,如果是*表示在结果中显示表中所有列

-在select后面的列名部分,可以使用as为列起别名,这个别名出现在结果集中

-如果要查询多个列,之间谁用都好分隔



2:消除重复行:

-在select后面列使用distinct可以消除重复的学生

Select distinct

gender from students;


3:条件:

-使用where语句对表中数据进行筛选,结果为true的行为会出现在结果集中

-语法:

Select *

from 表名 where 条件:


4:比较运算符:

Select *

from students where id>2;#查询students表中的id大于2的学生

Select *

from students where sname !=’李小璐’;#查询students表中姓名不是李小璐的学生

Select *from

studennts where isdelete=0;查询没被删除的学生,


5:逻辑运算符:

-and

-or

-not

Select *

from students where id >3 and gender=0;#查询编号大于三的女同学;

6:模糊查询:

       -like

       -%表示任意多个任意字符

       -_表示一个任意字符

Select *

from stu1 where sname like ‘陈%’;#查询stu1中姓名以陈开头的任意字符长度的学生;

Select *

from stu1 where sname like ‘陈_’;#查询stu1中姓名以陈开头且长度为2的学生;


7:范围查询

-in

Select * from stu1 where id in (1,7,8);#查询stu1中id为1,7,8的学生;

-between 。。。and。。。

       Select * fro stu1 where id between 3

and 8 and gender =1;#查询stu1中id在3到8之间的男生;

-判断查询:

-is null;

-is notnull;

Select from

stu1 where adress is not null and gender=1;查询填写了住址的男同学;

8: 优先级:

-小括号,not,比较预算符,逻辑运算符

-and比or先运算查询:

你可能感兴趣的:(数据库学习笔记_查询(2))