Mysql 查询 select

-- create table students (
-- studentNo varchar(10) primary key,
-- name varchar(10),
-- sex varchar(1),
-- hometown varchar(20),
-- age tinyint(4),
-- class varchar(10),
-- card varchar(20)
--  )
-- insert into students values
-- ('001', '王昭君', '女', '北京', '20', '1班', '340322199001247654'),
-- ('002', '诸葛亮', '男', '上海', '18', '2班', '340322199002242354'),
-- ('003', '张飞', '男', '南京', '24', '3班', '340322199003247654'),
-- ('004', '白起', '男', '安徽', '22', '4班', '340322199005247654'),
-- ('005', '大乔', '女', '天津', '19', '3班', '340322199004247654'),
-- ('006', '孙尚香', '女', '河北', '18', '1班', '340322199006247654'),
-- ('007', '百里玄策', '男', '山西', '20', '2班', '340322199007247654'),
-- ('008', '小乔', '女', '河南', '15', '3班', null),
-- ('009', '百里守约', '男', '湖南', '21', '1班', ''),
-- ('010', '妲己', '女', '广东', '26', '2班', '340322199607247654'),
-- ('011', '李白', '男', '北京', '30', '4班', '340322199005267754'),
-- ('012', '孙膑', '男', '新疆', '26', '3班', '340322199000297655')
-- where 条件查询

--    比较运算符
--     等于: =
--     大于: >
--     大于等于: >=
--     小于: <
--     小于等于: <=
--     不等于: != 或 <>
-- select age from students where name='小乔'   (查询小乔的年龄)
-- select * from students where age<20          (查询20岁以下的学生)
-- select * from students where hometown!='北京' (查询家乡不在北京的学生)
-- select card from students where studentNo='007'(查询学号是007的学生的身份证号)
-- select * from students where class!='1班' (查询一班以外的学生信息)
-- select name,sex from students where age>20 (查询年龄大于20的学生的姓名和性别)

-- 逻辑运算符
--
--     and
--     or
--     not

-- select * from students where age<20 and sex='女' (查询年龄小于20的女同学)
-- select * from students where sex='女' or class='1班' (查询女学生或'1班'的学生)
-- select * from students where not hometown='天津'   (查询非天津的学生)
-- select * from students where hometown='河南' OR hometown= '河北' (查询河南或河北的学生)
-- select from students where class='1班'and hometown='上海'  (查询'1班'的'上海'的学生 )
-- select * from students where not age=20   (查询非20岁的学生)

-- 模糊查询
--     like
--     %表示任意多个任意字符
--     _表示一个任意字符
--
-- select * from students where name like '孙%' (查询姓孙的学生)
-- select * from students where name like '孙_'(查询姓孙且名字是一个字的学生)
-- select * from students where name like '%乔'(查询叫乔的学生)
-- select * from students where name like '%白%'(查询姓名含白的学生)
-- select * from  students where name like '__'    (查询姓名为两个字的学生)
-- select * from students where name like '百%'and age>20 (查询姓百且年龄大于20的学生)
-- select * from students where studentNo like '%1' (查询学号以1结尾的学生)

-- 范围查询
--
--     in表示在一个非连续的范围内 between ... and ...表示在一个连续的范围内

-- select * from students where hometown in('北京','上海','广东')(查询家乡是北京或上海或广东的学生)
-- select * from students where age between 18 and 20(查询年龄为18至20的学生)
-- select *from students where age in(18,19,22)and sex='女' (查询年龄在18或19或22的女生)
-- select * from students where not age between 20 and 25 (查询年龄在20到25以外的学生)

-- 空判断
--
--     注意:null与''(字符串)是不同的
--     判空is null
--         判非空is not null

-- select * from students where card is null (查询没有填写身份证的学生)
-- select * from students where card ='' (查询身份证是空字符串的学生)
-- select * from students where card  is not null (填写身份证的学生)

-- 排序 order by
-- 默认按照列值从小到大排列
-- asc从小到大排列,即升序
-- desc从大到小排序,即降序
--
-- select * from students order by age(查询所有学生信息,按年龄从小到大排序)
-- select * from students order by age desc,studentNo (查询所有学生信息,按年龄从大到小排序,年龄相同时,再按学号从小到大排序)
-- select * from students order by class asc,studentNo (查询所有学生信息,按班级从小到大排序,班级相同时,再按学号再按学号从小到大排序)

-- 聚合函数  count(*)表示计算总行数 max最大 min 最小 sum求和 avg 平均

-- select count(*) from students;     (查询学生总数)
-- select max(age) from students where sex='女'(查询女生的最大年龄)
-- select min(age) from students where class=1(查询1班的最小年龄)
-- select sum(age) from students where hometown='北京'(查询北京学生的年龄总和)
-- select avg(age) from students where sex='女'(查询女生的平均年龄)
-- select max(age),min(age),avg(age)from students;(查询所有学生的最大年龄、最小年龄、平均年龄)
-- select count(*)from students where class=1; (一班共有多少个学生)
-- select count(*) from students where age<18 and class=3(查询3班年龄小于18岁的同学有几个)

-- 分组 group by 筛选分组数据 having 与where用法一样

-- select sex,count(*)from students group by sex 查询各种性别的人数
-- select age,count(*)from students group by age(查询各种年龄的人数)
-- select class,avg(age),max(age),min(age) from students group by class(查询各个班级学生的平均年龄、最大年龄、最小年龄)
-- select sex,count(*) from students group by sex having sex='男'(查询男生总人数)
-- select class,avg(age),max(age),min(age) from students group by class having class!=1(查询1班除外其他班级学生的平均年龄、最大年龄、最小年龄)
-- select class,avg(age),max(age),min(age) from students where class!='1班' group by class  (查询1班除外其他班级学生的平均年龄、最大年龄、最小年龄)

-- 分页 limit
-- select * from students limit 0,3(查询前3行学生信息)
-- select * from students limit 3,3 (查询第4到第6行学生信息)

-- 查询 顺序
-- select *
-- from 表名
-- where 条件1
-- group by 依据列
-- having 条件2
-- oeder by 依据列
-- limit 0,1
-- select class,max(age),min(age)from students where class !='1班' group by class order by class desc  limit 0,3

-- select * from students where name ='百里守约'(查询学生"百里守约"的基本信息)
-- select * from students where name ='百里守约'or name ='百里玄策' (查询学生"百里守约"或”百里玄策”的基本信息)
-- select name,age,class from students where name like '张%'(查询姓"张"学生的姓名,年龄,班级)
-- select *from students where name like '%约%'1(查询姓名中含有"约"字的学生的基本信息)
-- select studentno,name,age,class,card from students where name like '孙__' (查询姓名长度为三个字,姓“孙”的学生的学号,姓名,年龄,班级,身份证号)
-- select *from students where name like '百%' or '孙'(查询姓"百"或者姓”孙”的学生的基本信息)
-- select * from students where name like '百%'and hometown='山西'(查询姓"百"并且家乡是"山西"的学生信息)
-- select * from students where hometown in('北京','上海','新疆','山东') (查询家乡是"北京"、”新疆”、”山东”或者"上海"的学生的信息)
-- select * from students where name like '孙%'and hometown!='河北'(查询姓"孙",但是家乡不是"河北"的学生信息)
-- select * from students where hometown not in('北京','新疆','山东','上海') (查询家乡不是"北京"、"新疆"、"山东"、"上海"的学生的信息)
-- select * from students where hometown!='北京'and hometown!='上海'and hometown!='新疆'and hometown!='山东'(查询家乡不是"北京"、"新疆"、"山东"、"上海"的学生的信息)
-- select * from students order by sex (查询全部学生信息,并按照“性别”排序)
-- select name,hometown  from students group by hometown (查询现有学生都来自于哪些不同的省份)
-- select * from students group  by age having sex='男'(查询所有男生,并按年龄升序排序)
-- select  count(*) from students(统计共有多少个学生)
-- select * from students where age>20(统计年龄大于20岁的学生有多少个)
-- select avg(age)from students where sex='男'(统计男生的平均年龄)
-- select max(age) from students where class=1(、查询1班学生中的最大年龄是多少)
-- select sex,count(*) from students where class='2班' group by sex (统计2班男女生各有多少人)
-- select class,sex,count(*) from students group by class,sex order by class 1(统计每个班级中每种性别的学生人数,并按照班级升序排序)
-- select * from students order by age limit 1(查询年龄最小的学生的全部信息)
 




猜你喜欢

转载自blog.csdn.net/cyy19920319/article/details/81055324