- 基础查询:
1:查询所有列:SELECT * FROM tableName;
2:查询指定列:SELECT name, sex, phone FROM tableName;
3:查询结果去掉重复行:SELECT DISTINCT * FROM tabelName; select distinct name from tableName;
4:列运算:数字类型可以做 加,减,乘,除 运算: SELECT age*1.5 FROM tableName;
字符串类型用concat连接。 SELECT CONCAT( '我都名字:', name, '年龄是:',age) FROM tableName;
5:转换NULL值,如果列值为NULL相加时仍为NULL。 SELECT IFNULL(age, 0) FROM tableName;
如果age列中存在NULL值,那么当成0来算。
6:给列起别名:SELECT IFNULL(comm, 0) +1000 AS 奖金 FROM tableName; --AS 可省略。
- 条件查询 SELECT * FROM tableName
- 模糊查询 SELECT * FROM tabelName where name like '_张' ; '张_ _' ; '%张%';
- 排序:SELECT * FROM tabelName ORDER BY name ASC ; 升序!默认可省略。DESC 降序!
例如:select * from tableName order by name ASC , phone DESC;
- 聚合函数:select count(*) 人数,sum(sal) 总和,max(asl)最高,min(sal)最低,avg(sal)平均 from tableName;
- 分组查询:select sex , count(*) from tableName group by sex; 【sex ,可分组列】
SELECT sex ,count(*) from tableName where sal>15000 group by sex having count(*) >=20; 【性别人数大于20,工资大于15000】
执行顺序:select, from, where, group by, having, order by
- mysql 【方言】 limit : select * from tableName limit 0, 5; 查询tableName 中第一行到5行;
分页:一页10条,查询第3页: (3-1)* 10;【(当前页-1)* 每页记录数】