当前位置: 首页 > news >正文

CRUD操作-select

CRUD操作-select 基本查询(一)

-- CRUD操作
-- insert into
-- insert
-- replace intouse dbok;
show tables;
drop table if exists t5,t6,t_stu,t_student,t_teacher;-- 建立学生表
create table t_student(sid int unsigned auto_increment,sname varchar(50) not null,sage tinyint unsigned default 18,score int unsigned default 0,sbirth date,primary key (sid)
)select * from t_student;
insert t_student value(null,'李四',20,90,'1995-06-16');
insert into t_student set sname='李强',sbirth='2002-3-13';insert t_student(sbirth,sname) values('2001-10-12','王五'),('2001-10-12','王五'),('2001-10-12','王五');insert t_student(sname,sbirth)  select sname,sbirth from t_student;select count(*) from t_studentcreate table db1.t_student like t_student;
insert into db1.t_student select * from t_student;select count(*) from db1.t_student;create table a(`year` year,`month` tinyint,money int unsigned,primary key(`year`,`month`)
);insert into a value(2021,1,1000),(2021,2,1200),(2022,1,1300),(2022,2,1500);select * from a;select database();show tables;select max(sid) from t_student;delete from t_student where sid>1000;select count(*) from t_student;-- 清空表数据
delete from t_student;-- 清空表数据,保留表结构
truncate t_student;
-- 表(结构 + 数据)-- 查看
select * from t_student;update t_student set score = score + 5;update t_student set score= 85 ,sbirth='1996-3-3' ,sname='李四四' where sid = 7;-- select 查询
select * from t_student;
select sid,sname,sage,score,sbirth from t_student;select sname 姓名,sbirth as '成绩' from t_student;select sname,sid from t_student;select sid,sname,score+5 from t_student;select @@version,@@port,@@hostname,@@basedir,@@datadir,user(),database(),now();select host,user,authentication_string,plugin from mysql.user;select uuid(),uuid_short(),rand();
select uuid(),uuid_short(),rand() from dual;select year(curdate()),curdate(),curtime(),current_date;-- 查询条件
-- = > < >= <= !=  and &&   or ||  not !
select * from t_student where sid = 3 && sname like '李%';select * from t_student where sname != '李四';
select * from t_student where sname <> '李四';
select * from t_student where not sname = '李四';-- 查看  and or not
select * from t_student where not !false;-- 查询条件 is null 或 is not null
select * from t_student where sage = null;
select * from t_student where sage is null;
select * from t_student where sage is not null;
select * from t_student where not sage is null;-- 模糊查找条件 like % _
select *  from t_student where sname like '___';
select *  from t_student where sname like '%李%';
select *  from t_student where sname like '李__';select *  from t_student where sname = '%李%';-- 正则表达式
select '李四' regexp '[a-zA-Z]+';select * from t_student where sname regexp '^.*[a-zA-Z]+$';select * from t_student where sname regexp '[\\u4e00-\\u9fa5]{2}';

CRUD操作-select 集合函数 分组 分组条件 分组统计group by 查询结构排序order by(二)

-- 查询条件 between and   sage>=15 and sage <=19 此条件用于数字 和 日期
select * from t_student where sage between 15 and 19;-- <15 or >19
select * from t_student where sage not between 15 and 19;-- in   not in
select * from t_student where sid in (1,3,5,11,19);
select * from t_student where sid not in (1,3,5,11,19);-- 查询年龄最大的数字是
select max(sage) from t_student;select * from t_student where sage = max(sage);-- 子查询
select * from t_student where sage = (select max(sage) from t_student);-- 有多少人,平均年龄,最大 最小 总和
select count(*) 总人数,avg(sage) 平均年龄,max(sage) 最大年龄,min(sage) 最小年龄,sum(sage) 年龄总和
from t_student;alter table t_student add gender enum('男','女') default '男' after sname;
alter table t_student add sdept varchar(255) default '计算机科学';select count(*) from t_student st where st.gender = '男';
select count(*) from t_student st where st.gender = '女';
select count(*) from t_student st where st.gender is null;select rand();-- ifnull(null,1) ifnull(1,2)
select ifnull(null,1),ifnull(1,2),if(true,'yes','no'),if(rand()>.5,'yes','no');select sid 学号,sname 姓名,ifnull(gender,'') 性别 from t_student;-- 查询分组 根据gender分组  查询统计 男生多少人 女生多少人 保密多少人
select ifnull(gender,'保密') 性别,count(*) 人数,max(score) 最高分,min(score) 最低分
from t_student group by gender;select distinct sdept from t_student;select t_student.sdept 专业 ,count(*) 人数 from t_student group by sdept-- 统计优秀score>90多少人  良好score>80多少人  及格score>=60多少人 补考(score<60)多少
select slevel 级别,count(*) 人数 from (
select sid,sname,score,if(score>=90,'优秀',if(score>=80,'良好',if(score>=60,'及格','补考'))) slevel
from t_student where sdept='计算机科学') as st group by slevel having count(*)>2 order by count(*) desc;-- 排序 order by asc 不写默认升序 desc 降序
select sid,sname,score from t_student order by score desc,sname asc-- 随机查询两条记录
select t.*,rand() from t_student t order by rand() limit 2;
select * from t_student t order by rand() limit 5;select pi();

http://www.lryc.cn/news/118815.html

相关文章:

  • SD-WAN网络加速及应用场景分析
  • python机器学习(六)决策树(上) 构造树、信息熵的分类和度量、信息增益、CART算法、剪枝
  • eNSP:ospf和mgre的配置
  • 培训报名小程序-订阅消息发送
  • 资深测试员才知道的五个行业秘密
  • Ozone命令行接口详解
  • 机器学习笔记 - 基于C++的​​深度学习 二、实现卷积运算
  • python pandas 获取Excel文件下所有的sheet名称,表格数据
  • gateway做token校验
  • C#学习记录-线程
  • Spring Boot 启动注解分析
  • React Native数据存储
  • 【网络编程】揭开套接字的神秘面纱
  • MySQL 8.0 事务定义和基本操作
  • 项目经理必备:常用的项目管理系统推荐!
  • 【香瓜说职场】信任危机(2022.08.19)
  • 【Rust】Rust学习 第六章枚举和模式匹配
  • Win10安装GPU支持的最新版本的tensorflow
  • 20个Golang自动化DevOps库
  • 【WiFi】WiFi 6E最新支持的国家和频段
  • 如何使用html,包括css,js 画思维导图?有哪些可用的方法?
  • 机器学习---梯度下降代码
  • 【VB6|第23期】原来Jet.OLEDB也可以读取新版.xlsx的Excel文件
  • 通过控制ros节点的启停,软实现人工控制和紧急停止功能的图示
  • 面试热题(滑动窗口最大值)
  • 【代码】表格封装 + 高级查询 + 搜索 +分页器 (极简)
  • ant.design 组件库中的 Tree 组件实现可搜索的树: React+and+ts
  • Linux系统编程之信号(上)
  • 23.Netty源码之内置解码器
  • sigmoid ReLU 等激活函数总结