MySql数据库(二)

一、查询数据

(1) 查询所有字段
    select * from 表名;
    
(2) 查询指定字段的数据
    select 字段1,字段2 from 表名;
    如:
    select name,hometown from student;
    
 (3) 给字段起别名
    select 字段名 as 别名 from 表名;
    如:
    select name as 姓名,hometown as 家乡 from student;
    
 (4) 给表起别名
    select stu.name,stu.hometown from student as stu;
    
 (5) 消除重复行(distinct)
  select distinct sex from student;

二、where后面支持多种运算符,进行条件的处理

(1) 比较运算   (2)逻辑运算   (3)模糊查询(4)范围查询   (5)空判断

(1)比较运算
   查询学号是‘007’的学生身份证号
   select card from students where studentNo='007'

   查询‘1班’以外的学生信息
   select * from students where class!="1班"

   查询年龄大于20的学生的姓名和性别
   select name,sex from students where age>20

  (2) 逻辑运算符
       and                  or               not
       查询年龄小于20的女同学
       select * from students where age<20 and sex='女'

       查询 '1班'的'上海'的学生
       select * from students where class="1班" and  hometown="上海"
         
       查询非20岁的学生
       select * from students where not age=20

   (3) 模糊查询
       like   % 表示任意多个任意字符
              _ 表示一个任意字符

       查询姓名为两个字的学生
       select * from students where name like "__"
       
       查询姓'百'且年龄大于20的学生
       select * from students where name like '百%' and age>20

       查询学号以1结尾的学生
       select * from student where stuNo like "%1"

  (4) 范围查询
        in 表示一个非连续的范围内
		查询家乡是北京或上海或广东的学生
		方式一:
		    select * from studnents where howntown="北京" or howntown="上海" or hometown="广东"
		方式二:
		    select * from students where howntown in("北京","上海","广东")

        between...and...  表示在一个连续的范围内
		查询年龄18岁到20岁的学生
		方式一:
			select * from students where age>=18 and age<=20
		方式二:
			select * from students where age between 18 and 20

        查询年龄在20到25以外的学生
		select * from students where not age between 20 and 25

    (5) 空判断
		注意:null与''是不同的    null没有填写值,''空字符串,
		判断空: is null,
		判非空: is not null,
		查询没有填身份证的学生
		select * from students where stuNo is null
			
		查询填写了身份证的学生
		select * from students where stuNo is not null

三、排序

为了方便查看数据,可以对数据进行排序
	语法:
		select * from 表名 order by 列1 asc|desc,列2 asc|desc
		(1)将行数据安装列1进行排序,如果某些行列1的值相同时,则按照列2排序,以此类推
		(2)默认按照列值从小到大排序
		(3)asc 从小到大排序,即升序
		(4)desc 从大到小排序,即降序
		
		查询所有学生情况,按班级从小到大排序,班级相同时,再按学号从小到大进行排序
		select * from students order by class asc,stuNo asc
		
		查询所有学生信息,按照中文名字进行排序
		select * from students order by convert(name using gbk)  // convert 转换

四、聚合函数

为了快速得到统计数据,经常会用如下5个聚合函数
	(1) count(*) 表示计算总行数,括号中写*与列名,结果是相同的
	(2) max(列) 表示求此列的最大值
	(3) min(列) 表示求此列的最小值
	(4) sum(列) 表示求此列的和
	(5) avg(列) 表示求此列的平均值

    (1) 查询所有学生的最大年龄 最小年龄 平均年龄
	     select max(age) from students
		 select min(age) from students
		 select avg(age) from students

    (2) 查询1班共有多少学生?
	    select count(*) from students where class='1班'
    (3) 查询3班年龄小于18岁的同学有几个?
	    select count(*) from students where class='3班' and age<18

五、分组(group by)

按照字段分组,表示此字段相同的数据会被放到一个组中
	  语法:
	      select * from 表名 group by 列1,列2
		  
		  查询各个班级学生的平均年龄 最大年龄 最小年龄
		  select class,avg(age),max(age),min(age) from students group by class

六、分组后过滤

having 后面的条件运算符与where的相同
	  
查询1班其他班级学生的平均年龄 最大年龄 最小年龄
select class,avg(age),max(age),min(age) from students group by class having not class='1班'

七、多个字段分组

查询各个班级男女生的总数
select class,sex,count(*) from student group by class,sex

八、分页

语法:
	select  * from 表名 limit start,count
	从start开始,获取count条数据  start索引从0开始
		 
	查询4到6行的学生信息
	select * from student limit 3,3;
		 
	已知:每页显示m条数据,求:显示第n页的数据,
	select * from student limit (n-1)*m,m

九、连接查询

等值连接
方式一:生成临时表,然后再临时表中使用关联的字段进行过滤,
select * from student 表1,表2 where 表1.列=表2.列
		
方式二:(又称内连接):不生成临时表,连接时先判断条件,只有符合条件才会连接放到结果
select * from 表1 inner join 表2 on 表1.列=表2.列
		
查询年龄最小的学生
select * from students order by age asc limit 1
		
统计每个班级中每种性别的学生人数,并按照班级升序排列
select class,sex,count(sex) from students group by class,sex order by class
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值