技术标签: 查询mysql数据库实例名
name
age
yjh
23
zhong
21
bin
23
chao
24
0、建立表
create tablestudent(
idint(10) auto_increment primary key,
namevarchar(10) not null,
birthyear not null,
departmentvarchar(10) not null,
addressvarchar(10) not null);
格式是 列名 类型(长度) , 记得increment的拼写,不是increasement。 是varchar不是vchar
插入数据
insert into student values(null,'张老大',1985,'计算机系','北京市海淀区');
autoincrement的列给null就会自动从1开始递增
1、查找age最小的一个
普通SQL可使用top 1,但mysql没有top关键字select * from stu order by num limit 0,1;
也可以使用子查询select * from stu where num<=all(select * fromstu);
也可以使用min函数select * from stu where num=(select min(num) from stu);
2、查找年龄一样的人
首先用group by选择重复的num的集合,然后用in选择合适的行select * from stu where num in (select num from stu group by num having count(*)>1);
注意,使用了group by后,有关类聚的限定条件不再使用where,使用having
3、按成绩降序输出
注意降序的关键字,不是descendselect * from stu order by num desc;
student
+-----+--------+------+-------+------------+--------------+
| Id | Name | Sex | Birth | Department | Address |
+-----+--------+------+-------+------------+--------------+
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 |
| 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 905 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
+-----+--------+------+-------+------------+--------------+
score
+----+--------+--------+-------+
| Id | Stu_id | C_name | Grade |
+----+--------+--------+-------+
| 1 | 901 | 计算机 | 98 |
| 2 | 901 | 英语 | 80 |
| 3 | 902 | 计算机 | 65 |
| 4 | 902 | 中文 | 88 |
| 5 | 903 | 中文 | 95 |
| 6 | 904 | 计算机 | 70 |
| 7 | 904 | 英语 | 92 |
| 8 | 905 | 英语 | 94 |
| 9 | 906 | 计算机 | 90 |
| 10 | 906 | 英语 | 85 |
+----+--------+--------+-------+
1、查找英语系和计算机系人的信息
蠢的写法select * from student where Department='计算机系' or Department='英语系';
正常写法,使用集合select * from student where Department in ('计算机系','英语系');
2、选择18~22的学生信息
使用between and表示范围,now()函数显示当前时间,date_format()格式化输出select * from student where (select date_format(now(),'%Y'))-Birth between 18 and 22;
3、统计每个系的人数
select Department,count(*) from student group by Department;
4、选择每门课的最高分
使用group by,一般都会使用聚类函数select C_name,max(Grade) from score group by C_name;
5、选择李四的考试科目以及成绩
使用子查询select c_name,grade from score where Stu_id=(select Id from student where Name='李四');
6、链接两表查询所有学生信息和考试信息
select student.Id,Name,Sex,Address,Birth,Department,C_name,Grade from student,score where student.Id=Stu_id;
7、显示学生的总成绩
非聚类的条件还是用where,聚类的条件使用havingselect Name,sum(Grade) from score,student where student.Id=Stu_id group by Stu_id;
8、显示各科平均分
使用AS重命名列名select C_name,avg(Grade) as '平均分' from score group by C_name;
9、选择计算机低于95的学生信息
select * from student where Id in (select Stu_id from score where C_name='计算机' and Grade<95);
10、选择同时考英语和计算机学生的信息
查询同时XX时,不能用and,因为同一条记录不能同时是a又是b,只能是一条记录是a,另一条记录是b,而此两条记录都是属于同一个人。因此可以把是a的人的集合和是b的人的集合都选出来,然后再两个集合求交集select * from student where Id in (select distinct Stu_id from score where Stu_id in (select Stu_id from score where C_name='计算机') and Stu_id in (select Stu_id from score where C_name='英语'));
也可以先选出符合一个条件的集合,再从此集合中选择符合第二个条件的select * from student where Id in (select Stu_id from score where Stu_id in (select Stu_id from score where C_name='计算机') and C_name='英语');
或者使用两个相同的表,分别与学生表链接并规定范围select a.* from student a,score b,score c where a.Id=b.Stu_id and a.Id=c.Stu_id and b.C_name='计算机' and c.C_name='英语';
11、查询计算机考试成绩并从高到低排序
select * from score where C_name='计算机' order by Grade desc;
12、从两个表查询学生学号,并合并
unionall直接合并两个查询结果SELECT id FROM student union all SELECT stu_id FROMscore;
union合并后排序并去除重复记录,效率比union all低SELECT id FROM student union SELECT stu_id FROM score;
mysql只有union一个并集操作,差集交集使用in和not in自行实现
交集select * from score where stu_id in (select stu_id from score where c_name='计算机') and c_name='英语';
差集select * from score where stu_id not in (select stu_id from score where c_name='计算机') and c_name='英语';
13、查找姓王和姓张的同学的姓名、院系、考试科目及成绩
普通like关键字,使用%代表不定长字符,_代表单个字符select name,department,c_name,grade from student,score where stu_id=student.id and (name like '张%' or name like '王%');
rlike可使用正则表达式select name,department,c_name,grade from student,score where stu_id=student.id and (name rlike '张*' or name rlike '王*');
14、查询所有来自湖南学生的个人信息及考试信息
select name,department,c_name,grade from student,score where stu_id=student.id and address rlike '湖南*';
15、使用exists
exists表明子查询的结果集大于1条
mysql> select name from student where exists (select * from student where birth=1988) and name='王六';
16、group by多个字段
先按dept分组,组内按job分组select dept,job,max(salary) from factory group by dept,job;
17、order by 可用第n个字段的n代替字段名
两者是一样的select Stu_id ,Grade from score order by 2;select Stu_id ,Grade from score order by Grade asc; //asc是升序,默认就是升序,所以一般很少用。降序desc
文章浏览阅读1.5w次,点赞9次,收藏26次。简介一个创建某个文件到桌面快捷方式的BAT批处理.代码@echooff::设置程序或文件的完整路径(必选)setProgram=D:\Program Files (x86)\格式工厂.4.2.0\FormatFactory.exe::设置快捷方式名称(必选)setLnkName=格式工厂v4.2.0::设置程序的工作路径,一般为程序主目录,此项若留空,脚本将..._批处理创建桌面快捷方式
文章浏览阅读2k次。射频识别技术漫谈(6-10),概述RFID的通讯协议;射频ID卡的原理与实现,数据的传输与解码;介绍动物标签属性与数据传输;RFID识别号的变化等_芯片 ttf模式
文章浏览阅读1.1k次。今天小编心血来潮,为大家带来一个很有趣的项目,那就是使用 Python web 框架 Django 来实现支付宝支付,废话不多说,一起来看看如何实现吧。_django 对接支付宝接口流程
文章浏览阅读842次。Zabbix 5.0 LTS,跑了一年多了一直很稳定,前两天空间显示快满了,于是手贱清理了一下history_uint表(使用mysql truncate),结果折腾了一周。大概故障如下:然后zabbix论坛、各种群问了好久都没解决,最后自己一番折腾似乎搞定了。初步怀疑,应该是由于历史数据被清空后,zabbix需要去处理数据,但是数据量太大,跑不过来,所以来不及更新了(?)..._zabbix问题没有更新
文章浏览阅读296次。一、数据结构之字典 key-value
文章浏览阅读9.7k次,点赞3次,收藏13次。最近项目中遇到一个问题,是关于mybatis-plus的字段注解策略,记录一下。1问题调用了A组件(基础组件),来更新自身组件的数据,发现自己组件有个字段总是被清空。2原因分析调用的A组件的字段,属于基础字段,自己业务组件,对这个基础字段做了扩展,增加了业务字段。但是在自己的组件中的实体注解上,有一个注解使用错误。mybatis-plus封装的updateById方法,如果..._mybatisplus strategy
文章浏览阅读3.8k次。如何构建倒排索引,我们将这个过程叫做“索引构建”。如果我们的文档很多,这样索引就一次性装不下内存,该如何构建。硬件的限制 我们知道ram读写是随机的操作,只要输入相应的地址单元就能瞬间将数据读出来或者写进去。但是磁盘不行,磁盘必须有一个寻道的过程,外加一个旋转时间。那么只有涉及到磁盘,我们就可以考虑怎么节省I/O操作时间。【注】操作系统往往以数据块为单位进行读写。因为读一_为某一文档及集构件词项索引时,可使用哪些索引构建方法
文章浏览阅读836次。英特尔技术与制造事业部副总裁卞成刚7日在财富论坛间隙接受中新社记者采访时表示,该公司看好中国市场前景,扎根中国并以此走向世界是目前最重要的战略之一。卞成刚说,目前该公司正面临战略转型,即从传统PC服务领域扩展至所有智能设施领域,特别是移动终端。而中国目前正引领全球手机市场,预计未来手机、平板电脑等方面的发明创新将大量在中国市场涌现,并推向全球。持相同态度的还有英特尔中国区执行董事戈峻。戈峻
文章浏览阅读627次。https://blog.csdn.net/zrs19800702/article/details/53101213http://blog.csdn.net/lzw06061139/article/details/51445311https://my.oschina.net/linuxhunter/blog/654080rgw 概述Ceph 通过radosgw提供RES..._radosgw -c
文章浏览阅读3.7k次,点赞6次,收藏9次。我为什么选择ECharts ? 本周学校课程设计,原本随机佛系选了一个51单片机来做音乐播放器,结果在粗略玩了CN-DBpedia两天后才回过神,课设还没有开始整。于是懒癌发作,碍于身上还有比赛的作品没交,本菜鸡对硬件也没啥天赋,所以就直接把题目切换成软件方面的题目。写python的同学选择了一个时间序列数据的可视化曲线程序设计题目,果真python在数据可视化这一点性能很优秀。..._echarts 时间序列
文章浏览阅读1.6k次。事件类:/** * * * @className: EarlyWarnPublishEvent * * @description:数据风险预警发布事件 * * @param: * * @return: * * @throws: * * @author: lizz * * @date: 2020/05/06 15:31 * */public cl..._applicationeventpublisheraware
文章浏览阅读1.2k次。如需转载请注明出处!点击小图片转到图片查看的页面在Android开发中很常用到,抱着学习和分享的心态,在这里写下自己自定义的一个ImageView,可以实现类似微信朋友圈中查看图片的功能和效果。主要功能需求:1.缩放限制:自由缩放,有最大和最小的缩放限制 2居中显示:.若图片没充满整个ImageView,则缩放过程将图片居中 3.双击缩放:根据当前缩放的状态,双击放大两倍或缩小到原来 4.单指_imageview图片边界回弹