MySQL--数据库查询、条件查询、模糊查询、范围查询、空判断

---数据的准备

	 -- 创建一个数据库
       create database python_test charset=utf8;
       create database 'python-04' charset=utf8;     --当有特殊字符时 要引起来。
       
     --使用一个数据库
     use python_test;
     
	 --显示当前使用的数据库
	 select database():

	 --创建一个数据表班级
	create table classes(
     id int unsigned not null auto_increment primary key,
     name varchar(20) not null
     );
     
     --创建一个数据表学生
      create table students(
   id int unsigned primary key not null auto_increment,
     name varchar(20) default '',
     age tinyint unsigned default 0,
     height decimal(5,2) default 0,
     gender enum('男','女','未知') default'未知',
     class_id int unsigned default 0,
     is_delete bit default 0
     );
	 --向students表中插入数据 ,这里直接复制,符号请忽略。
	 mysql> insert into students values
    -> (0, '小明', 18, 180.00, 1, 1, 0),
    -> (0, '小月', 18, 170.00, 2, 1, 0 ),
    -> (0, '刘德华',59, 180.00,1,2,0),
    -> (0, '彭于晏', 30, 175.00, 1, 3, 1),
    -> (0, '周杰伦',31,175.00,1,3,0),
    -> (0, '刘亦菲', 30, 175.00, 2, 4, 0),
    -> (0, '周姐', 25, 170.00, 2,5,1);
	 
	 --向classes表中插入数据
    mysql> insert into classes values
    -> (0, 'python_01期'),(0, 'python_02'),(0, 'python_03');

-- 查询

	--查询所有字段
	select * from students;
	select * from classes;
	
	-- 查询指定字段
	select id,name from students;
	select students.id, students.name from students;
	select s.id, s.name from students as s;
	
	--给指定字段起名字,并查询
	select name as 姓名, age as 年龄 from students;
	
	--去重  distinct
	select distinct gender from students;
	
--条件查询  where 条件

	--比较运算符
			--大于18岁的信息
			select * from students where age>18;
			
			--小于18岁的信息
			select * from students where age<18;
			
			--等于18岁的信息
			select * from students where age=18;
			
			--不等于18岁的信息
			select * from students where age!=18;
		
	--逻辑运算符  and or not
			--18-28岁之间的学生
			select * from students where age>18 and age<28;
			
			--18岁以上的女性
			select * from students where age>18 and gender=2;
			
			--18岁以上 或者 身高超过180(包括180)
			select * from students where age>18 or height>=180;
			
			--年龄不是小于或者等于18  并且是女性
			select * from students where not age<=18 and gender=2;
			
	--模糊查询  like rlike
			--like  %替换一个或者多个  _替换一个
			
					-- 查询名字中以'小'开头的
					 select name from students where name like '小%';
					 
					 --查询名字中带  小  的
					 select name from students where name like '%小%';
					 --查询名字是两个字的
					 select name from students where name like '__';  -- 两个下划线
					 
					 --查询名字至少是两个字的
					 select name from students wherre name like '__%';


			--rlike  正则表达式
				    --查询以  周 开始的姓名
				    select name from students where name rlike '^周.*';
				    
					--查询以周开头 以伦结尾的 姓名
					select name from students where name rlike '^周.*伦$';
			
	--范围查询
			--in(1,2,3)表示在一个非连续的范围之内
					--查询年龄为18,34 的学生的姓名
					select name from students where age in (18,34);
					
			--not in (1,2,3) 表示不在一个非连续的范围之内
					--查询年龄不为18,34 的学生的姓名
					select name from students where age not in (18,34);

			--between... and ...  表示在一个连续的范围之内
					-- 查询年龄在 30-35之间的学生姓名
					select name from students where age between 30 and 35;
					
			--not between ...and ...   表示不在一个连续的范围之内
					--查询年龄b不在 30-35之间的学生姓名
					 select name from students where age not between 30 and 35;
					 
	--空判断
			--is null
					-- 查询身高为空的信息
					select * from students where height is null;
					
			--not is null
					--查询身高不为空的信息
					select * from students where height is not  null;
					
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值