2.根据条件查询指定的数据
Select * from 表名 where 列名1=值 and 列名2=值....
例:Select * from stu where sid=9 and ssex='女';
3.查询数据,返回指定的列
Select 列名1,列名2 from stu;
例:Select sid,sname from stu;
4.给指定返回列取别名(小名)
语法1. Select 列名 别名,列名2 别名2... from 表名;
例:Select sid id,sname name from stu;
语法2. Select 列名 as 别名,列名2 as 别名2... from 表名;
例:Select sid as id,sname as name from stu;
5.在条件中使用比较运算符
SELECT * FROM 表名 where 字段 > < >= <= !=或<>
例:select * from stu where xsnianling !=18;
6.多条件的查询:
AND OR NOT
例:select * from stu where xsnianling <=21 and xsxingbie='女';
例:select * from stu where xsnianling <21 or xsxingbie='女';
例:select * from stu where xsnianling not in(18,21,25);
7.对空值的查询:is null 对应列是否null查询
例:select * from stu where xsxueli is not null;
例:select * from stu where xsxueli is null;
8.BETWEEN A AND B 在A和B之间,包含AB的值
例:select * from stu where xsnianling BETWEEN 18 and 21;
9.IN
例:select * from stu where xsnianling in(18,21,25);
10.模糊查询 LIKE
%:指代不明确值的位置或长度
_:指代明确值的位置或已知字符串长度
例:select * from stu where xsxingming like '_灵%'
11.查询中使用算术表达式:+ - * /
例:select xsxuehao+xsnianling from stu where xsxingming like '_灵%'
12.处理重复值:DISTINCT 排除重复展示,只展示一次
例:select DISTINCT xsxingbie from stu;
14.通过查询复制表
create table stu1 select * from stu;
--只复制结构
例:create table stu2 select * from stu where 1=2;
--复制旧表的数据到新表(假设两个表结构一样)
例:insert into stu2 select from stu;
--复制旧表的数据到新表(假设两个表结构不一样)
例:insert into stu2(字段1,字段2,…….) SELECT 字段1,字段2,…… FROM stu;
15.分组 group by
例:select ssex,COUNT(*) from stu GROUP BY ssex
注意:分组使用的时候group by字段一定要在select后面出现,如果使用了group by,select 后面就不要出现 *
16.排序 order by 字段名:字段名就是我们需要排序的字段
order by xsnianling 升序 ASC 默认
order by xsnianling desc 降序
17.多个排序条件,当第一个条件相同时,以第二个条件排序
例:select * from stu order by age desc,createDate desc;
18.虚拟表
在没有表被引用的情况下,允许您指定dual作为一个假的表名
例:select 1+1 from dual;
四.事务控制语言(TCL:Transaction Control Language)
(如commit、rollback语句)