MySQL数据库 - sql语句基础大全

#注意 ‘(单引号)是sql语句里的特殊符号,必须转义,使用斜杠转义   select ’ab\'c‘ from tablename

#1 查询students表的所有记录
SELECT * FROM students;

#2 查询students表中的所有记录的sname,ssex和class列
SELECT sname,ssex,class FROM students;

#3 查询编号为101的同学记录
SELECT * FROM students WHERE sno = 101;

#4 查询性"王"的同学记录  (% 通配符,以王开始,后面几个都行    _匹配具体的字符个数)
SELECT * FROM students WHERE sname LIKE '王%';
SELECT * FROM students WHERE sname LIKE '王_';

#5 查询不性"王"的同学记录
SELECT * FROM students WHERE sname NOT LIKE '王%';

#6 查询 scorses表中成绩在60到80之间的所有记录
SELECT * FROM scores WHERE degree BETWEEN 60 AND 80;
SELECT * FROM scores WHERE degree >=60 AND degree <= 80;

#7 查询scorse表中成绩为85,86或88的记录
SELECT * FROM scores  WHERE degree=85 OR degree=86 OR degree=88;
SELECT * FROM scores WHERE degree IN(85,86,88);

#8 查询"95031"班的"男"同学的记录
SELECT * FROM students where calss='95031' AND ssex='男';

#9 查询"95031"班或性别为"女"的同学记录
SELECT *FROM students WHERE class = '95031' OR ssex='女';

#10 查询teacher表所有的系即不重复的depart列
SELECT DISTINCT depart FROM teachers;

#11 以班级class降序查询students表的所有记录
SELECT * FROM students ORDER BY class DESC

#12 以cno升序,degree降序查询scorse表的所有记录
SELECT * FROM scorse ORDER BY cno,degree DESC

#13 查询"95031"班的学生人数
SELECT COUNT (*) FROM students WHERE class = '95031';

#14 查询每个班的学生人数
#分组后,选择字段只能写两种
#1,聚合函数  2 分组中出现字段
SELECT class,COUNT(*) FROM students GROUP BY class;

#15 查询大于2名男生的班号
SELECT class,COUNT(*) FROM students WHERE ssex='男' 
GROUP BY class HAVING COUNT(*) > 2 ;
#where条件 它和groupby搭配

#16 查询最高分
SELECT MAX(degree) FROM scores ;

#17 查询最低分 最高分 平均分 总分
SELECT MIN(degree) FROM scores;
SELECT MIN(degree),MAX(degree),AVG(degree),SUM(degree) FROM scores;

#18 查询scores表中的最高分的学生号和课程号
SELECT cno,sno FROM scores WHERE degree = (SELECT MAX(degree) FROM scores);

#19 查询'3-105'号课程的平均分
SELECT cno,AVG(degree),ROUND(AVG(degree),1) FROM scores GROUP BY cno 
HAVING cno = '3-105';

#20 查询各科的平均分
SELECT cno,ROUND(AVG(degree),2) FROM  scores GROUP BY cno;

#21 查询最低分大于70,最高分小于90的sno列
SELECT * FROM scores WHERE degree>70 AND degree <90;

#22 查询95033班和95031班全体学生的记录
SELECT * FROM students WHERE class = '95033' OR class= '95031';
#union  把两个结果集相加
SELECT  sno,sname as s1 FROM students where class='95033'
union
SELECT sno,sname as s2 FROM students WHERE class='95031'

#23 查询存在有85分以上成绩的课程cno
SELECT cno FROM scores WHERE degree>85;

#24 查询所有教师teachers和同学students的name,sex和birthday
SELECT tnema naem,tsex sex,tbirthday birthday FROM  teachers 
UNION
SELECT sname,ssex,sbirthday FROM students;

#25 查询所有"女"教师和"nv"同学的naem,sex和birthday
SELECT tname,tsex,tbirthday FROM teachers WHERE tsex ="女"
UNION
SELECT sname,ssex,sbirthday FROM students WHERE ssex = "女";

#26 查询所有任课教师的tname 和 depart
课程表courses中有老师编号tno的就算任课
SELECT tname,depart FROM teachers WHERE tno IN(SELECT DISTINCT tno FROM courses);

#27 查询所有未任课的教师的tname和depart
SELECT tname,depart FROM teachers WHERE tno NOT IN(SELECT DISTINCT tno FROM courses);

#28 查询同名的同学记录
先插入测试数据:
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS)
VALUES(110,'王芳','女','1978-02-10',95031);
SELECT sname FROM students GROUP BY sname HAVING COUNT(*) >1
SELECT * FROM students WHERE sname = (
    SELECT sname FROM  students GROUP BY sname HAVING COUNT(*)>1)
    
#29 查询学生的姓名和年龄 = 当前年-birthday年
SELECT sname,sbirthday,YEAR(now())-year(sbirthday) AS age FROM students;

#30 查询学生中最大和最小的出生年月
SELECT MIN(sbirthday),MAX(sbirthday) FROM students;

#31 以班号和年龄从大到小的顺序查询student表中的全部记录
SELECT * FROM students ORDER BY class DESC,YEAR(NOW())-YEAR(sbirthday) DESC

#32 查询男教师及其所上的课程
SELECT t.tname,c.cname FROM (SELECT *FROM teachers WHERE tsex='男')t
LEFT JOIN (SELECT * FROM courses)c 
ON t.tno = c.tno;
#简
SELECT t.tname,c.cname FROM teacher t 
LEFT JOIN courses c
ON t.tno= c.tno
WHERE t.sex = '男'

#33 查询各门课程的最高分同学的sno,cno和degree列
SELECT sno,cno,MAX(degree) FROM scores GROUP BY cno;

#34 查询课程对应的老师姓名 , 职称,.所属系
SELECT cname,tnaem,prof,depart FROM 
(SELECT tno,cname FROM courses)a
LEFT JOIN
(SELECT tno,tname,prof,depart FROM teachers)b
ON a.tno = b.tno;

#35 查询课程对应的女老师姓名 .职称,所属系
SELECT cname,tnaem,prof,depart FROM
(SELECT tno,cname FROM courses) a
LEFT JOIN 
(SELECT tno,tname,prof,depart,tsex FROM teachers) b
ON a.tno = b.tno
WHERE tsex ='女'

行列转置,SQL没有真正实现,即使实现效率也不高,但是毕竟是SQL直接写成,不是应用代码

#36 行列转置:各科最好成绩
分组方式:
SELECT cno,MAX(degree) degree FROM scores GROUP BY cno;
执行结果:6-106,6-166
行列转置:
SELECT 
MAX(CASE cno WHEN '3-105' THEN degree ELSE 0 END) '3-105',
MAX(CASE cno WHEN '3-245' THEN degree ELSE 0 END) '3-245',
MAX(CASE cno WHEN '6-106' THEN degree ELSE 0 END) '6-106',
MAX(CASE cno WHEN '6-166' THEN degree ELSE 0 END) '6-166'
 FROM scores;

#37 行列转置:每个学员各科最好成绩
每个学员(分组);各科成绩(行列转置);最好成绩(MAX函数)
SELECT   sno,
MAX(CASE cno WHEN '3-105' THEN degree ELSE 0 END) '3-105',
MAX(CASE cno WHEN '3-245' THEN degree ELSE 0 END) '3-245',
MAX(CASE cno WHEN '6-106' THEN degree ELSE 0 END) '6-106',
MAX(CASE cno WHEN '6-166' THEN degree ELSE 0 END) '6-166'
 FROM scores
GROUP BY sno;

#38 行列转置:课程编号和课程名称
SELECT cno,cname FROM courses;
#ground_concat
SELECT 
    REPLACE(GROUP_CONCAT(CASE cno WHEN '3-105' THEN cname ELSE '' END),',','') AS '3-105',
    REPLACE(GROUP_CONCAT(CASE cno WHEN '3-245' THEN cname ELSE '' END),',','') AS '3-245',
    REPLACE(GROUP_CONCAT(CASE cno WHEN '6-106' THEN cname ELSE '' END),',','') AS '6-106',
    REPLACE(GROUP_CONCAT(CASE cno WHEN '6-166' THEN cname ELSE '' END),',','') AS '6-166',
    REPLACE(GROUP_CONCAT(CASE cno WHEN '9-888' THEN cname ELSE '' END),',','') AS '9-888'
FROM courses;
SELECT DISTINCT
    REPLACE(GROUP_CONCAT(CASE cno WHEN '3-105' THEN cname ELSE '' END),',','') AS '3-105',
    REPLACE(GROUP_CONCAT(CASE cno WHEN '3-245' THEN cname ELSE '' END),',','')  AS '3-245',
    REPLACE(GROUP_CONCAT(CASE cno WHEN '6-106' THEN cname ELSE '' END),',','')  AS '6-106',
    REPLACE(GROUP_CONCAT(CASE cno WHEN '6-166' THEN cname ELSE '' END),',','')  AS '6-166',
    REPLACE(GROUP_CONCAT(CASE cno WHEN '9-888' THEN cname ELSE '' END),',','')  AS '9-888'
FROM courses;

#39 课程号"3-105"的前3名学员排行
SELECT * FROM scores
WHERE cno = '3-105'
ORDER BY DEGREE DESC
LIMIT 3

#40 课程"计算机导论"的前3名学员排行
SELECT * FROM scores
WHERE cno = (SELECT cno FROM courses WHERE cnaem='计算机导论')
ORDER BY degree DESC
LIMIT 3

#41 课程号"3-105"的倒数最后3名学员排行
SELECT * FROM scores WHERE cno='3-105' ORDER BY degree LIMIT 3
ORDER BY degree desc;

#42 upper把值转成大写 lower把值转成小写    lcase把值转成小写 ucase把值转成大写
SELECT NAME,UPPER(NAME),LOWER(NAME) , LCASE(NAME), UCASE(NAME)FROM stt; 
 
#43 length获取字段值
SELECT NAME,LENGTH(NAME) FROM stt;

#44 substr(x,y,z)函数用来 截取数据    把x字段,从y位置开始,要截取z位
SELECT NAME,SUBSTR(NAME ,1,2),SUBSTR(NAME,2,3)FROM stt;

#45 concat(m,n)函数用来,拼接数据 --可以避免安全问题   把m字段的值.拼接上n的值
SELECT NAME ,CONCAT(NAME,'dsf') FROM stt

#46 replace(x,y,z)函数用来 替换值,  把x字段的值,进行替换,把y字段换成z的内容,把ename字段里的a字符换成b
SELECT NAME,REPLACE(NAME,'h','b')FROM stt;

#47 round函数用来 对数据四舍五入取整                  ceil向上取整,floor向下取整
SELECT passwort,ROUND(passwort),CEIL(passwort),FLOOR(passwort) FROM stt

#48 获取当前时间
SELECT NOW(),CURDATE(),CURTIME(),YEAR(NOW()),MONTH(NOW()),DAY(NOW()),HOUR(NOW()),MINUTE(NOW()),SECOND(NOW());
#年月日时分秒    年月日    时分秒    年    月    日    时    分    秒

#49 条件永远成立
select * from emp where 1=1;

#50 查询是null的记录
SELECT * FROM stt WHERE NAME IS NULL;
#    查询不是null的记录
SELECT * FROM stt WHERE NAME IS NOT NULL;

#51 获取指定位置数据
SELECT *FROM stt LIMIT 2;       //获取前两条数据
SELECT *FROM stt LIMIT 0,2;     //从第0条开始(第一条),获取2条数据

#52 按照名字排序,按照字母顺序排序  (null 在最前面)
SELECT * FROM stt ORDER BY NAME

#53 ifnull(m,0)函数  m是字段名  0是要替换成的内容 如果字段查出的值有空则替换成0
SELECT  IFNULL (NAME,0) FROM stt;

#54 统计年薪  过滤null的元素,否则无法正常运算,的不到结果
SELECT *,ifnull(passwort,0)*12+passwort*12 as 年薪 FROM stt 

#55 使用having 给group by后的结果加过滤条件 不是空的就查出来
SELECT *,COUNT(passwort) FROM stt GROUP BY passwort HAVING NAME IS NOT NULL 

#56 ground by配合 having 使用 
SELECT age,AVG(NAME) A FROM `new_tablename` GROUP BY age HAVING A <30
#别名 在where 里不许用  HAVING 换成 where 报错

#57 多个 LEFT JOIN 查询
SELECT a.wareid,a.ware_material_time,d.orderid,a.extend2,a.ware_type,a.ware_procedure,a.ware_action,c.case_rfid,d.box_rfid,d.sum , d.materialcode FROM 
wareinfomation a LEFT JOIN Vehicle_cabinet b 
ON a.extend2 = b.cabinet_rfid 
LEFT JOIN Vehicle_cases c 
ON c.case_rfid = b.case_rfid 
LEFT JOIN Vehicle_Box d 
ON d.box_rfid = c.box_rfid 
WHERE d.orderid=? AND a.ware_procedure=? AND a.ware_action='2' AND a.ware_type IN(2,3)

#58 指定一张表插入一条数据
INSERT INTO `sapinterface_log`(DATA,description,HOST) VALUES('{"加油 加油"}','加油','加油')

#59 今天减去出生年查询年龄
SELECT sname,sbirthday,YEAR(NOW()-YEAR(svirtaday)) AS age FROM students;

#60 查询所有数据名
SHOW DATABASES

#61更新表Student_Scores中的Pass_Or_Fail列,值为“Pass”(如果成绩为>= 60)或值为“Fail”(如果成绩< 60),且该生的专业必须为“软件工程”。
UPDATE Student_Scores SET Pass_Or_Fail=CASE WHEN Score >= 60 THEN 'Pass'
WHEN Score < 60 THEN 'Fail'
END WHERE  Course_Name = '软件工程';

人无完人,接受指点与反驳,森!!仔

你可能感兴趣的:(数据库,sql,mysql,数据库)