mysql数据库查询参考

mysql -uroot -p123456先进入数据库

看一下数据库名字 我们数据插入在student_db2 所以进入student_db2 然后查表名

show databases;

use student_db2;

show tables;

1.create index Index_Score on sc (Score ASC);

ASC表示升序

2.create unique index Index_Cname on course (Cname DESC);

unique 代表唯一索引 DESC代表降序

3.create index Index_Grade_Spe on student(grade ASC, specialty DESC);

先后顺序先后传参即可

4.需要查询student表的数据 先查询所有数据确定列名跟数据类型

select * from student;

然后使用month函数提取生日中的月份跟4比对即可

select * from student where month(birthday) = 4;

5.直接比对sno前四位跟grade前四位是否相等即可

select * from student where left(sno,4) <> left(grade,4);

使用left函数提取前四位

还可以使用select * from student where left(sno,4) <> replace(grade,'级','');

题目不要所有的信息,只要学号姓名专业年级

那把*修改为具体列名即可

select sno,sname,specialty,grade from student where left(sno,4) <> replace(grade,'级','');

另一种方法同理

6.select sno,sname,timestampdiff(year,birthday,en_time) from student where timestampdiff(year,birthday,en_time) <= 18;

使用timestampdiff函数 计算

第三列名字太长了 给他起个名字

select sno,sname,timestampdiff(year,birthday,en_time) as age from student where timestampdiff(year,birthday,en_time) <= 18;

这样就好看多了

7.select * from student where sname like '%玲';

用通配符%去模糊匹配即可

只要学号姓名性别修改*即可

select sno,sname,ssex from student where sname like '%玲';

8.用到course表 先查询一下course表的所以数据跟列名

select * from course;

至于题目要求查询 运算即可

select * from course where classhour/credit != 4;

  1. select * from course where cname = '数据结构' or cname='操作系统';

select * from course where cname in ('数据结构','操作系统');

直接查询即可

posted @ 2025-10-21 01:21  fortune_h2c  阅读(4)  评论(0)    收藏  举报