1查询所有的列
select *from student
2查询指定列
select name,age from student
3查询时候使用别名
select name as 别名,age as 年龄 from student as可以省略
4查询增加常量列
//查询的时候加上一列专业
select id,name,age,addr,'就业办' as 专业 from student
5查询合并列
select name,(math+english) as 总成绩 from student
select name+addr from student //此时不能合并
6查询去重
select distinct age from student'
select distinct(age) from student
7条件查询
(1)and or
select *from student where name="李四" and age = 30
(2)查询范围
select *
from student
where 1=1
and math>70
(3)between
select *from student where math between 78 and 98
(4)查询为空的记录 、
SELECT * FROM student;
-- 增加备注列
ALTER TABLE student ADD COLUMN remarks VARCHAR(50);
-- 给sid为1的学生添加一个“新增加”备注
UPDATE student SET remarks='新增加' WHERE sid=1;
-- 给sid为2的学生设置空字符串
UPDATE student SET remarks='' WHERE sid=2;
-- 需求: 查询备注不为空的学生(包括空字符串和null)
SELECT * FROM student WHERE remarks IS NULL OR remarks='';
(5)模糊查询
a:使用like关键字
B:%代表任意字符
c:_代表一个字符
-- 需求: 查询姓'天',且名字只有两个字的学生
SELECT * FROM student WHERE sname LIKE '天_';
-- 注意:null:数据没有修改过 空字符串: 被修改过
SELECT * FROM student WHERE remarks IS NOT NULL AND remarks!='';
8查询排序(默认为增序)
select *from student order by 列名 asc//升序
select *from student order by age desc//降序
多个条件排序
按照年龄升序,按照servlet成绩降序
SELECT * FROM student ORDER BY age ASC,math DESC;
9注意注意 查询返回限定行
(1)查询返回限定行
select *from student limit 4;//返回行数
//查询第3 4行的数据
注意:第一个参数:查询的起始行(从0开始算的)
第二个参数:查询的行行素
//查询第一二行的数据
select *From student limit 0,2
10 使用聚合函数查询
(1)查询math成绩最高
select max(math) from student
(2)查询jsp平均成绩
select avg(jsp) from student;
11 分组查询
//查询人数大于2的小区重庆万州
select address,count(sid) 人数 from student GROUP BY address HAVING COUNT(sid) >2