1、查询表中所有的信息:
Select * from 表名;
2、查询表中部分列的信息:
select 列名1,列名2,列名3... from 表名;
3、给列起别名:
select 列名1 新列名,列名2 新列名, 列名3 新列名... from 表名;
4、去重 distinct:(重复显示的数据(一模一样的),只显示一行,其余的不显示)
select distinct 列表1,列表2,列表3... from 表名;
5、带条件的查询:
select * from 表名 where [查询条件];
- 单一查询条件运算符:= != < <= > >=
Select * from 表名 where 列名=” ”;
- 多条查询条件:and or
Select * from 表名 where 列名=’ ’ and 列名<’ ’;
Select * from 表名 where 列名=’ ‘ or 列名>=’ ‘;
多条件运行算符:in, between ‘值1’ and ‘值2’, not(取反)
Select * from 表名 where 列名 in(‘ ‘,’ ‘,… )
Select * from 表名 where 列名 between ‘值1’ and ‘值2’;
Select * from 表名 where not(列名=’ ‘/列名between ‘值1’ and ‘值2’);
6、模糊查询like:
(like不可单独使用,必须搭配通配符’%’或’_’一起使用)
%代表0个或多个字符,_代表任意一位字符,占位符
Select * from 表名 where 列名 like ‘%x%y_’;
7、查询结果排序order by(ASC升序,DESC降序):默认升序
Select * from 表名 where 列名=’ ‘ order by 列名 desc;
Select * from 表名 where 列名 like ‘%x%y_’ and 列名in(‘ ‘,’ ‘) order by 列名1 ACS,列名2 DESC;
8、将两个列连接起来显示 concat:
Select concat (name,’的薪水是’,sal) from 表名; xxx的薪水是多少
9、查询空值 is null:
NULL 空值,不确定的值,未知的值,运算符只能是 IS,别的运算符都不可以, 非空: NOT NULL ,运算符也是 IS
Select * from 表名 where 列名 is null/is not nll;
特点:空值和任何值做运算,结果还是空值;
Select sal+comm from 表名; 查询表中总收入
Select name,sal+ifnull(comm,0) from 表名; -- 空值转换函数 ifnull(参数1,参数2),作用,让空值变成一个确定的值,实际的值
10、指定查看的记录数 limit num:
Select * from 表名 limit n;
查看指定位置:
Select * from 表名 limit m,n; 跳过m行,从m+1行开始显示,至第m+1+n行