高级查询语句
1.模糊查询
Like 用于在where 子句中进行模糊查询,SQL like 子句中使用%来表示任意0个或者多个字符,下划线 _ 表示任意一个字符。
使用LIKE子句从数据表中读取数据的通用语法:
select field1,field2,...fieldn from table_name where field1 like condition1
e.g. select * from cls where name like '%A%' ;查找名字里含有A的所有记录
2. as用法
在sql语句中as 用于给字段或者表重命名,特别是一些名字比较长的表或者字段,可以简化表和字段的表达方式。
e.g. select name as 姓名,age as 年龄 from cls;
select * from cls as c where c.age>17;
3. order by 排序: 默认是升序 asc 降序 desc
4. limit 限制:
limit子句用于限制由 select 语句返回的数据数量 或者 update ,delete语句的操作数量带有limit子句的select 语句的基本语法如下:
select * from table_name where field limit [num];
5.union 联合查询:
union 操作符用于连接两个以上的select 语句的结果组合到一个结果集合中。多个select 语句会删除重复的数据。
union 操作符语法格式:
select expression1,expression2,...expression_n from tables [where conditions]
union [All|distinct]
select expression1,expression2,...expression_n from tables [where conditions];
默认情况下union 操作符已经删除了重复数据,所以distinct修饰符对结果没啥影响。如果是ALL则返回所有结果集,包含重复数据。
6.子查询
定义:当一个select 语句中包含另外一个select查询语句,则称为有子查询的语句
子查询出现的位置:
1.from 之后:select name from (select * from cls where sex='m') as s where s.score>90;
2.where 子句中,此时select 查询到的内容为外层查询的条件值
select * from cls where age= (select age from cls where name ='Tom');
浙公网安备 33010602011771号