基本语法
select * from tablename [where conditions];
1.条件查询
条件一般使用=、>、>=、<、<=、!=等比较运算符或is、is not、in、not in等,使用and、or等逻辑运算符连接多个条件联合查询;
@查询部门编号>=1且薪水>=3000的记录。
select * from info where deptno >=1 and sal >= 3000;
2.查询不重复的记录
使用关键字distinct,要查出的几个字段全部放在它后面,注意该关键字前面不能有字段;
@查询英文名和部门编号不重复的记录。
select distinct ename,deptno from info;
3.排序和限制
基本语法如下:select * from tablename [where conditions] [order by field1 [asc\desc],field2 [asc\desc],...]
排序的字段越靠前,优先级越高。
@将查出的记录按照部门编号升序
select * from info order by deptno asc;
@将查出的记录按照部门编号升序、入职日期降序
select * from info order by deptno asc,hiredate desc;
对于排序后的记录,我们希望展示其中一部分,这时将用到limit关键字,写在语句最后面。
具体语法是:select ... limit [offset_start,]row_count;
其中offset_start表示记录的起始偏移量(默认是0,可以不写),row_count表示显示的行数;
@查出按照薪水升序的前3条记录
select * from info order by sal limit 0,3;
@查出按照薪水升序且第2条开始的3条记录
select * from info order by sal limit 1,3;
4.聚合
基本语法:select field1,field2,field3,...fieldn func_name from tablename [where connditions] [group by field1,field2,...fieldn] [with rollup] [having where_condition];