mysql多表子查询


SELECT * FROM stu;

#添加多行数据
INSERT INTO stu VALUES(1,'aaa'),(2,'ccc'),(3,'小明');
DROP TABLE stu_temp;
CREATE TABLE stu_temp
(
    sid INT PRIMARY KEY,
    NAME VARCHAR(200)
)
SELECT * FROM stu_temp;
#1:stu_temp表 不能存在的
#2:一边取数据一边创建数据表
#3:stu_temp 约束(主键)是从stu中拿不出来的
CREATE TABLE stu_temp(SELECT * FROM stu);
SELECT * FROM info;
#4:只需要某些数据表的基本结构,不需要数据!
# 查询过程中 where条件不成立,查询的就是结构,否则就是数据+结构
CREATE TABLE info_temp(SELECT * FROM info WHERE 1=2);

SELECT * FROM info WHERE 1=1;

SELECT * FROM info_temp;

CREATE TABLE info_temp3(SELECT id,age FROM info);

SELECT * FROM info_temp3;

DELETE FROM info_temp3;

SELECT * FROM info;

#通过age排序 1:升序 2:降序
#1:升序  从小到大  asc :不写的情况,默认就升序
SELECT * FROM info ORDER BY age ASC;
#2:降序  从大到小  desc
SELECT * FROM info ORDER BY age DESC;
#select 字段  表名  where 过滤条件  order by 排序;
SELECT * FROM info WHERE age>18 ORDER BY age DESC;


SELECT t.`id` AS 编号,t.`name` 姓名,t.`age` 年龄 FROM info t;
# 只要出现+,会发生两个字段之间的运算
SELECT t.id+'.'+t.`age` AS 连接后 FROM info t;

#null:空对象  而不是空白字符串
SELECT * FROM borrow WHERE returndate IS NULL;
#not : 不是的
SELECT * FROM borrow WHERE returndate IS NOT NULL;


#聚合函数
SELECT * FROM score;
#最高分 max()
SELECT MAX(t.`sexam`) AS 最高分 FROM score t;
#最低分 min()
SELECT MIN(t.`sexam`) AS 最低分 FROM score t;
#平均分 avg()
SELECT AVG(t.`sexam`) AS 平均分 FROM score t;
#总行数
SELECT COUNT(*) AS 行数 FROM score;
#总和
SELECT SUM(t.`sexam`) AS 总分成绩 FROM score t;

#字符串函数
#1:拼接字符串函数
SELECT CONCAT('jack','and','rose','一起吃个饭!') AS 字符串;

SELECT * FROM student;
#数据表中,是不区分大小写
SELECT CONCAT(t.`StudentName`,t.`LoginPwd`,t.`Address`) 字符串 FROM student t;

SELECT * FROM student;
#select 都是虚拟结果,不会对原始数据表产生影响
#2:insert函数 替换
SELECT INSERT(t.`Address`,2,3,'abc') AS 替换 FROM student t;


#3:大小写转换
SELECT UPPER(LOWER(UPPER(t.`Email`))) 转换 FROM student t;

#4:截取字符串
SELECT SUBSTRING('abcefghgk',2);
SELECT SUBSTRING('abcefghgk',2,3);

#日期函数
#1:当前日期
SELECT CURDATE();

#2:当前时间
SELECT CURTIME();

#3:当前日期和时间
SELECT NOW();

#4:一年中的第几周
SELECT WEEK(NOW());

#5:年份
SELECT YEAR(NOW());

#6:月份
SELECT MONTH(NOW());
SELECT * FROM student;

SELECT YEAR(t.`BornDate`) AS 年份,MONTH(t.`BornDate`) AS 月份 FROM student t;


#7:差值  datediff: 天数
SELECT DATEDIFF(NOW(),'2002-02-02');

#数据表中计算年龄
SELECT DATEDIFF(NOW(),a.`BornDate`)/365 AS 生日 FROM student a;

SELECT FLOOR(DATEDIFF(NOW(),a.`BornDate`)/365) AS 生日 FROM student a;

#8:计算日期增量
SELECT ADDDATE(NOW(),50000);

SELECT ADDDATE(NOW(),500000);

#数学函数
#向上取整
SELECT CEIL(RAND()*10);
#向下取整
SELECT FLOOR(RAND()*10);

#limit:分页
SELECT * FROM student;
/*
    分页
    每页显示3条数据,分3页
    公式:(当前页-1)*每页显示条数
    select * from 表名 limit 公式,每页显示的条数;
    limit:限制行数
*/
#显示第1页
SELECT * FROM student LIMIT 0,3;
#显示第2页?(当前页-1)*每页显示条数
SELECT * FROM student LIMIT 3,3;

SELECT * FROM student LIMIT 6,3;

#查询比“谢挺疯”年龄大的学生信息
#用一个sql查询出来,需要子查询
#子查询执行的原理:
#先执行子语句,执行完成后,把查询的结果给父查询
#从上往下写,先要结果,后要过程
SELECT * FROM student t WHERE t.`BornDate`<(
    SELECT borndate FROM student WHERE studentname='谢挺疯'
);

SELECT * FROM student;
SELECT * FROM score;
SELECT * FROM SUBJECT;

/*
查询参加最近一次Logic Java考试成绩的学生的最高分和最低分
1:最近
2:Logic Java
100  22
*/
SELECT MAX(sexam) 最高分,MIN(sexam) 最低分 FROM score WHERE subjectno=(
    SELECT subjectno FROM SUBJECT WHERE subjectname='Logic Java')
AND sdate =(
    SELECT MAX(sdate) FROM score WHERE subjectno=(
        SELECT subjectno FROM SUBJECT WHERE subjectname='Logic Java')
);
#优化后:数据库性能优化
SELECT MAX(sexam) 最高分,MIN(sexam) 最低分 FROM score WHERE sdate =(
    SELECT MAX(sdate) FROM score WHERE subjectno=(#根据Logic java查询
        SELECT subjectno FROM SUBJECT WHERE subjectname='Logic Java')
);
SELECT * FROM student;
SELECT * FROM score;
SELECT * FROM SUBJECT;
#查询“Logic Java”课程考试成绩为60分的学生名单
#in多个结果返回给父级查询
SELECT * FROM student WHERE studentno IN (
    SELECT studentno FROM score WHERE subjectno IN(
        SELECT subjectno FROM SUBJECT WHERE subjectname='Logic Java'
    ) AND sexam=60
)
#not:取反
SELECT * FROM student WHERE studentno NOT IN (
    SELECT studentno FROM score WHERE subjectno IN(
        SELECT subjectno FROM SUBJECT WHERE subjectname='Logic Java'
    ) AND sexam=60
)

#查询参加“Logic Java”课程最近一次考试的在读学生名单
SELECT * FROM student WHERE studentno IN(
    SELECT studentno FROM score WHERE subjectno IN(
        SELECT subjectno FROM SUBJECT WHERE subjectname = 'Logic Java'
    ) AND sexam=(
        SELECT MAX(sexam) FROM score WHERE subjectno IN(
            SELECT subjectno FROM SUBJECT WHERE subjectname = 'Logic Java'
        )
    )
)

SELECT * FROM student WHERE studentno NOT IN(
    SELECT studentno FROM score WHERE subjectno IN(
        SELECT subjectno FROM SUBJECT WHERE subjectname = 'Logic Java'
    ) AND sexam=(
        SELECT MAX(sexam) FROM score WHERE subjectno IN(
            SELECT subjectno FROM SUBJECT WHERE subjectname = 'Logic Java'
        )
    )

)



mysql多表子查询_第1张图片


#房屋所在区县、 街道为至少有2个街道有房屋
SELECT a.`SID`,b.`SDID`,COUNT(*) AS num FROM hos_house a
INNER JOIN hos_street b ON a.`SID`=b.`SID`
GROUP BY a.`SID`,b.`SDID`;

#显示:户型,姓名,区县,街道
SELECT (SELECT htname FROM hos_type WHERE htid=t.`HTID` ) AS 户型,
(SELECT uname FROM sys_user WHERE uid=t.`UID`) AS 姓名,
(SELECT dname FROM hos_district WHERE did=(SELECT sdid FROM hos_street WHERE t.`SID`=sid)) AS 区县,
(SELECT sname FROM hos_street WHERE t.`SID`=sid) AS 街道 FROM hos_house t
WHERE t.`SID` IN(
    SELECT sid FROM hos_street WHERE sdid IN(
        SELECT m.sdid FROM (
            SELECT a.`SID`,b.`SDID` AS sdid,COUNT(*) AS num FROM hos_house a
            INNER JOIN hos_street b ON a.`SID`=b.`SID`
            GROUP BY a.`SID`,b.`SDID`
        ) m
        GROUP BY m.sdid
        HAVING COUNT(*)>1
    )
);




#1:每一个统计 季度  区县  街道  户型 算出房屋的数量
#2:按照区县小计
#3:按照季度合计
SELECT QUARTER(a.`HTIME`) AS 季度,
d.`DName` AS 区县,
b.`SName` AS 街道,
c.`HTName` AS 户型,COUNT(*) AS 数量 FROM hos_house a
INNER JOIN hos_street b ON a.`SID`=b.`SID`
INNER JOIN hos_type c ON a.`HTID`=c.`HTID`
INNER JOIN hos_district d ON b.`SDID`=d.`DID`
GROUP BY QUARTER(a.`HTIME`),d.`DName`,b.`SName`,c.`HTName`
UNION
SELECT QUARTER(a.`HTIME`) AS 季度,
d.`DName` AS 区县,
' 小计','',COUNT(*) AS 数量 FROM hos_house a
INNER JOIN hos_street b ON a.`SID`=b.`SID`
INNER JOIN hos_type c ON a.`HTID`=c.`HTID`
INNER JOIN hos_district d ON b.`SDID`=d.`DID`
GROUP BY QUARTER(a.`HTIME`),d.`DName`
UNION
SELECT QUARTER(a.`HTIME`) AS 季度,
' 合计','','',COUNT(*) AS 数量 FROM hos_house a
INNER JOIN hos_street b ON a.`SID`=b.`SID`
INNER JOIN hos_type c ON a.`HTID`=c.`HTID`
INNER JOIN hos_district d ON b.`SDID`=d.`DID`
GROUP BY QUARTER(a.`HTIME`)
ORDER BY 1,2,3,4;

你可能感兴趣的:(mysql)