mysql 查詢

Student(Sid,Sname,Sage,Ssex) 學生表 Sid:學號;Sname:學生姓名;Sage:學生年齡;Ssex:學生性別
Course(Cid,Cname,Tid) 課程表 Cid,課程編號;Cname:課程名字;Tid:教師編號
SC(Sid,Cid,score) 成績表 Sid:學號;Cid,課程編號;score:成績
Teacher(Tid,Tname) 教師表 Tid:教師編號; Tname:教師名字mysql

一、查詢1課程比2課程成績高的全部學生的學號;sql

SELECT
    a.Sid
FROM
    (SELECT Sid,score FROM sc WHERE Cid = 1) a,
    (SELECT Sid,score FROM sc WHERE Cid = 2) b
WHERE
    a.score > b.score
AND a.Sid = b.SId;

需求:查詢學號數據庫

條件:比較成績(同一學生)編程

對同一張表進行內鏈接查詢數組

二、查詢平均成績大於60分的同窗的學號和平均成績;函數

SELECT
    Sid,
    AVG(score)
FROM
    sc
GROUP BY
    Sid
    HAVING AVG(score) > 60;

需求:查詢學號和平均成績(定位主查成績表)oop

條件:比較平均成績學習

按學號分組查詢平均成績,注意涉及到分組字段條件關鍵字用HAVING,其餘字段條件在分組前用wherespa

三、查詢全部同窗的學號、姓名、選課數、總成績;3d

SELECT
    student.Sid,
    student.Sname,
    COUNT(sc.Cid),
    SUM(sc.score)
FROM
    student
LEFT JOIN sc ON student.Sid = sc.Sid
GROUP BY student.Sid,student.Sname

 

需求:查詢學生信息,成績信息(定位學生表、成績表)

條件:無

count函數,sum函數,學生id,學生name,經過對學生id,學生name分組,學生表左連接成績表查出相關信息

四、查詢姓「語」的老師的個數;

 

SELECT
    COUNT(teacher.Tid)
FROM
    teacher
GROUP BY
    teacher.Tid,teacher.Tname
HAVING
    teacher.Tname LIKE '語%';


SELECT
    COUNT(DISTINCT(Tname))
FROM
    teacher
WHERE
    Tname LIKE '語%';

 

需求:求教師的個數(教師表,聚合函數count)

條件:姓‘語’(like ‘語%’)

HAVING語句一般與GROUP BY語句聯合使用,用來過濾由GROUP BY語句返回的記錄集。

HAVING語句的存在彌補了WHERE關鍵字不能與聚合函數聯合使用的不足。

SELECT
    COUNT(teacher.Tid)
FROM
    teacher
WHERE teacher.Tname="語文老師"
GROUP BY
    teacher.Tid
HAVING
    teacher.Tid = 1

五、查詢沒學過「語文老師」課的同窗的學號、姓名

 

SELECT
    student.Sid,
    student.Sname
FROM
    student
WHERE
    student.Sid NOT IN (
        SELECT
            DISTINCT(sc.Sid)
        FROM
            sc,
            teacher,
            course
        WHERE
            sc.Cid = course.Cid
        AND teacher.Tid = course.Tid
        AND teacher.Tid = "語文老師"
    );

 

需求:查詢學生信息(學生表,教師表,課程表,成績表-關聯學生)

條件:沒學過語文老師的課程(沒... not in)學過語文老師的課程

六、查詢學過「1」而且也學過編號「2」課程的同窗的學號、姓名;

 

SELECT
    student.Sid,
    student.Sname
FROM
    student,
    sc
WHERE
    student.Sid = sc.Sid
AND sc.Cid = 1
AND EXISTS (
    SELECT
        *
    FROM
        sc AS sc2
    WHERE
        sc2.Sid = sc.Sid
    AND sc2.Cid = 2
)

 

需求:查詢學生信息(學生表,成績表)

條件:學過「1」而且也學過編號「2」課程(不用查主表,應爲條件爲id因此關聯表成績表,同一字段兩個條件:條件1 and  exists(條件2 and 表1條件字段=表2條件字段))

七、查詢學過「語文老師」所教的全部課的同窗的學號、姓名;

SELECT
    student.Sid,
    student.Sname
FROM
    student
WHERE
    student.Sid IN (
        SELECT
            sc.Sid
        FROM
            sc,
            course,
            teacher
        WHERE
            sc.Cid = course.Cid
        AND course.Tid = teacher.Tid
        AND teacher.Tname = "語文老師"
        GROUP BY
            sc.Sid
        HAVING
            COUNT(sc.Cid) = (
                SELECT
                    COUNT(course.Cid)
                FROM
                    course,
                    teacher
                WHERE
                    course.Tid = teacher.Tid
                AND teacher.Tname = "語文老師"
            )
);

需求:查詢學生信息(學生表,教師表,成績表,課程表)

條件:學過「語文老師」所教的全部課,學過語文老師的課+學過語文老師的全部課(數據表id in 內鏈接 條件1 分組 條件2 count  內鏈接 條件1)

八、查詢課程編號「2」的成績比課程編號「1」課程低的全部同窗的學號、姓名;

 


SELECT
    student.Sid,
    student.Sname
FROM
    student,
    (
        SELECT
            sc.Sid,
            sc.score
        FROM
            sc
        WHERE
            sc.Cid = 1
    ) a1,
    (
        SELECT
            sc.Sid,
            sc.score
        FROM
            sc
        WHERE
            sc.Cid = 2
    ) a2
WHERE
    student.Sid = a1.Sid
AND a1.Sid = a2.Sid
AND a1.score > a2.score; SELECT ret.Sid, ret.Sname FROM ( SELECT student.Sid, student.Sname, sc.score, ( SELECT sc2.score FROM sc sc2 WHERE sc2.Sid
= student.Sid AND sc2.Cid = 2 ) score2 FROM student, sc WHERE student.Sid = sc.Sid AND sc.Cid = 1 ) ret WHERE ret.score2 < ret.score

 

需求:查詢學生信息(學生表,成績表)

條件:課程1成績比課程2成績高

課程1的成績比課程2的成績做比較,由於中間表成績表中有課程id和學生id因此不會用到課程表,主查詢表爲學生表,同一學生不一樣課程成績的比較。

不一樣課程的成績可從中間表查詢學生id和比較的分數其結果做爲中間表a1,一樣中間表a2,兩表和主表內聯查詢,最後where做比較

一箇中間表既包括學生信息,又包括兩個分數信息,可是第二個分數須要作子查詢和主表關聯。最後條件比較兩個分數

九、查詢全部課程成績小於60分的同窗的學號、姓名;

 

SELECT
    student.Sid,
    student.Sname
FROM
    student
WHERE
    student.Sid NOT IN (
        SELECT DISTINCT
            (student1.Sid)
        FROM
            sc,
            student student1
        WHERE
            sc.Sid = student1.Sid
        AND sc.score > 60
    )

需求:查詢學生信息(學生表,成績表)

條件:全部課程成績小於60

說明:全部小於 = 不大於,因此要用 not in 大於一個數的查詢用來簡化條件

十、查詢沒有學全全部課的同窗的學號、姓名;

 

SELECT
    student.Sid,
    student.Sname
FROM
    student,
    sc
WHERE
    student.Sid = sc.Sid
GROUP BY
    student.Sid,
    student.Sname
HAVING
    COUNT(sc.Cid) < (
        SELECT
            COUNT(course.Cid)
        FROM
            course
    )

 

需求:查詢學生信息(學生表,課程表,成績表)

條件:沒有學全全部課

說明:全部課和學完了的課作比較,連個數量比較,屬於一我的的兩個聚合關係的比較,因此要按人的其餘非聚合信息分組,在having中比較聚合關係

十一、查詢至少有一門課與學號爲「1」的同窗所學相同的同窗的學號和姓名;

 

SELECT DISTINCT
    student.Sid,
    student.Sname
FROM
    student,
    (SELECT sc.Sid, sc.Cid FROM sc) sc1,
    (
        SELECT
            sc2.Sid,
            sc2.Cid
        FROM
            sc sc2
        WHERE
            sc2.Sid = 1
    ) sc3
WHERE
    student.Sid = sc1.Sid
AND sc1.Cid = sc3.Cid;

SELECT
    DISTINCT
    student.Sid,
    student.Sname
FROM
    student,
    sc
WHERE
    student.Sid = sc.Sid
AND sc.Cid IN (
    SELECT
        sc1.Cid
    FROM
        sc sc1
    WHERE
        sc1.Sid = 1
);

需求:查詢學生信息(學生表,成績表)

條件:至少有一門課與學號爲「1」的同窗所學課程相同

說明:主表爲學生表,主表id做爲關聯,單條數據中的課程id in 學號爲1所學課程的id,或者子查詢全部課程和學號1所學課程內鏈接,須要注意的是結果笛卡爾積,須要使用distinct去重

十二、查詢至少學過學號爲「1」同窗全部課的其餘同窗學號和姓名;

 

1三、把「SC」表中「語文老師」老師教的課的成績都更改成此課程的平均成績;

 

UPDATE sc
INNER JOIN (
    SELECT
        a.*
    FROM
        (
            SELECT
                sc2.Cid,
                AVG(sc2.score) average
            FROM
                sc sc2
            GROUP BY
                sc2.Cid
        ) a,
        teacher,
        course
    WHERE
        a.Cid = course.Cid
    AND teacher.Tid = course.Tid
    AND teacher.Tname = "語文老師"
) AS b ON sc.Cid = b.Cid
SET sc.score = b.average

 

UPDATE sc
SET sc.score = (
	SELECT
		AVG(sc1.score)
	FROM
		sc AS sc1,
		course AS course1,
		teacher AS teacher1
	WHERE
		sc1.Cid = course1.Cid
	AND course1.Tid = teacher1.Tid
	AND teacher1.Tname = "語文老師"
)
WHERE
	sc.Cid IN (
		SELECT
			course.Cid
		FROM
			course,
			teacher
		WHERE
			course.Cid = teacher.Tid
		AND teacher.Tname = "語文老師"
	);
[Err] 1093 - You can't specify target table 'sc' for update in FROM clause

 

需求:批量修改某老師課程的成績表中成績爲此課程的平均成績

條件:語文老師所教的課

說明:批量修改通常使用set 屬性=值 where id in 數組的操做,可是這樣作得在編程中將條件查詢成常量,在進行二次查詢,不然出現上述錯誤,在子查詢中不能用被更新的表。mysql 中可使用update a join b on a.id=b.id set a.name=b.name where a.m='...' and b.n='...'

1四、查詢和「1」號的同窗學習的課程徹底相同的其餘同窗學號和姓名;

 

SELECT
    student.Sid,
    student.Sname,
    COUNT(sc.Cid)
FROM
    student,
    sc
WHERE
    student.Sid = sc.Sid AND sc.Cid IN (SELECT Cid FROM sc WHERE Sid = 1)
GROUP BY
    Sid,Sname
HAVING
    COUNT(1) = (
        SELECT
            COUNT(1)
        FROM
            sc
        WHERE
            Sid = 1
    )

 

 

 

需求:查詢學生信息(學生表,成績表)

條件:和同窗1的課程徹底相同的其餘學生的信息

說明:學生1的課程信息 = 其餘學生的信息。這個條件進行拆分:其餘同窗的課程id in 學生1的課程id && 其餘同窗的課程count = 學生1的課程數量

1五、刪除學習「語文老師」課的SC表記錄;

 

 

DELETE
FROM
    sc
WHERE
    sc.Cid IN (
        SELECT
            sc1.Cid
        FROM
            sc sc1,
            course,
            teacher
        WHERE
            sc1.Cid = course.Cid
        AND course.Tid = teacher.Tid
        AND teacher.Tname = "語文老師"
    );

[Err] 1093 - You can't specify target table 'sc' for update in FROM claus

 

DELETE sc
FROM
    sc,
    course,
    teacher
WHERE
    sc.Cid = course.Cid
AND course.Tid = teacher.Tid
AND teacher.tName = "語文老師"
AND sc.score = 33;

 

 

需求:刪除成績表記錄(成績表,老師表)

條件:學習過語文老師課的成績

說明:修改操做不能再子查詢中使用被修改的表,因此第一種寫法錯誤,第二種寫法中delete a from a,b,c .... 這裏被刪除的a必須在from後邊有出現過,不然語法錯誤

1六、向SC表中插入一些記錄,這些記錄要求符合如下條件:沒有上過編號「3」課程的同窗學號、二、號課的平均成績;

 

INSERT Sid,1,(SELECT AVG( score) FROM sc WHERE Cid=1) FROM student WHERE Sid NOT IN(SELECT Sid FROM sc WHERE Cid=1)

 

需求:插入成績信息(學生表,成績表)

條件:沒上過課程3的學生,平均成績。先查出課程,按照課程id程批量插入。 主查詢,子查詢 + not in

1七、按平均成績從高到低顯示全部學生的「語文」、「數學」、「英語」三門的課程成績,按以下形式顯示: 學生ID,語文,數學,英語,有效課程數,有效平均分

 

SELECT
    t.Sid AS '學生id',(
        SELECT
            score
        FROM
            sc
        WHERE
            Sid = t.Sid
        AND Cid = 1
    ) AS '語文成績',
    (
        SELECT
            score
        FROM
            sc
        WHERE
            Sid = t.Sid
        AND Cid = 2
    ) AS '數學成績',
    (
        SELECT
            score
        FROM
            sc
        WHERE
            Sid = t.Sid
        AND Cid = 3
    ) AS '英語成績',
    COUNT(1) AS '有效課程數',
    AVG( t.score) AS '平均成績'
FROM sc AS t GROUP BY t.Sid ORDER BY AVG(t.score)

需求:學生ID,語文,數學,英語,有效課程數,有效平均分(sc)

條件:按平均成績從高到低顯示,主查詢中課程id做爲子查詢的條件來查各課程的成績,最後主查詢按照學生id分組,按照平均成績排序

1八、查詢各科成績最高和最低的分:以以下形式顯示:課程ID,最高分,最低分

SELECT
    Cid AS '課程id',
    MAX(score) AS '最高成績',
    MIN(score) AS '最低成績'
FROM
    sc
GROUP BY
    Cid;

 

SELECT
    Cid AS '課程id',
    MAX(score) AS '最高成績',
    MIN(score) AS '最低成績'
FROM
    sc,student WHERE sc.Sid = student.Sid
GROUP BY
    Cid;



SELECT
    DISTINCT
    l.Cid,
    l.score,
    r.score
FROM
    sc l,
    sc r
WHERE
    l.Cid = r.Cid
AND l.score = (
    SELECT
        MAX(ll.score)
    FROM
        sc ll,
        student lm
    WHERE
        l.Cid = ll.Cid
    AND lm.Sid = ll.Sid
    GROUP BY
        ll.Cid
)
AND r.score = (
    SELECT
        MIN(lr.score)
    FROM
        sc AS lr
    WHERE
        r.Cid = lr.Cid
    GROUP BY
        lr.Cid
);

需求:詢各科成績最高和最低的分(sc,student)

條件:

說明:各科說明按課程id分組,最低分,最高分都在成績表中,因此使用max,min能夠直接查出,同時也能夠自關聯經過條件子查詢max,min進行等於比較進行條件判斷來查詢相關信息。

1九、按各科平均成績從低到高和及格率的百分數從高到低順序

 

SELECT
    sc.Cid,
    course.Cname,
    AVG(sc.score) AS '平均成績',
    100 * SUM(
        CASE
        WHEN IFNULL(sc.score, 0) >= 60 THEN
            1
        ELSE
            0
        END
    ) / COUNT(1) AS '及格百分比'
FROM
    sc,
    course
WHERE
    sc.Cid = course.Cid
GROUP BY
    sc.Cid
ORDER BY
    AVG(sc.score) ASC,
    100 * SUM(
        CASE
        WHEN IFNULL(sc.score, 0) >= 60 THEN
            1
        ELSE
            0
        END
    ) / COUNT(1)
 DESC

 

 

 

需求:查詢課程id,課程名稱,平均成績,及格百分數(成績表,課程表)

條件:各科平均成績正序,及格率倒序

說明:成績表關聯課程表,按照課程id,課程名稱分組,及格百分比經過及格課程數/課程總數得到 case when else end, ifnull

20、查詢以下課程平均成績和及格率的百分數(用"1行"顯示): 語文(1),數學(2),英語(3

需求:查詢各課程平均成績,及格率

條件:一條記錄查詢各課程平均成績,及格率

說明:查詢兩個字段,兩個函數計算結果得出一條記錄,因此都用子查詢,以主表某字段做爲子查詢條件,%能夠說使用concat ( left (數值1 / 數值2 *100,5),'%')先截取字符串,在連接字符串,同時截取字符串能夠替換爲四捨五入等操做

SELECT
    SUM(
        CASE
        WHEN Cid = 1 THEN
            score
        ELSE
            0
        END
    ) / SUM(CASE WHEN Cid = 1 THEN 1 ELSE 0 END) AS '語文平均分',
    100 * SUM(
        CASE
        WHEN Cid = 1
        AND score >= 60 THEN
            1
        ELSE
            0
        END
    ) / SUM(CASE WHEN Cid = 1 THEN 1 ELSE 0 END) '語文及格率',
    SUM(
        CASE
        WHEN Cid = 2 THEN
            score
        ELSE
            0
        END
    ) / SUM(CASE WHEN Cid = 2 THEN 1 ELSE 0 END) AS '數學平均分',
    100 * SUM(
        CASE
        WHEN Cid = 2
        AND score >= 60 THEN
            1
        ELSE
            0
        END
    ) / SUM(CASE WHEN Cid = 2 THEN 1 ELSE 0 END) '數學及格率',
    SUM(
        CASE
        WHEN Cid = 3 THEN
            score
        ELSE
            0
        END
    ) / SUM(CASE WHEN Cid = 3 THEN 1 ELSE 0 END) AS '英語平均分',
    100 * SUM(
        CASE
        WHEN Cid = 3
        AND score >= 60 THEN
            1
        ELSE
            0
        END
    ) / SUM(CASE WHEN Cid = 3 THEN 1 ELSE 0 END) '英語及格率'
FROM sc

SELECT
    SUM(
        CASE
        WHEN Cid = 1 THEN
            score
        ELSE
            0
        END
    ) / SUM(CASE WHEN Cid = 1 THEN 1 ELSE 0 END) AS '語文平均分',
    CONCAT(
        LEFT (
            100 * SUM(
                CASE
                WHEN Cid = 1
                AND score >= 60 THEN
                    1
                ELSE
                    0
                END
            ) / SUM(CASE WHEN Cid = 1 THEN 1 ELSE 0 END),
            5
        ),
        '%'
    ) '語文及格率',
    SUM(
        CASE
        WHEN Cid = 2 THEN
            score
        ELSE
            0
        END
    ) / SUM(CASE WHEN Cid = 1 THEN 1 ELSE 0 END) AS '數學平均分',
    CONCAT(
        LEFT (
            100 * SUM(
                CASE
                WHEN Cid = 2
                AND score >= 60 THEN
                    1
                ELSE
                    0
                END
            ) / SUM(CASE WHEN Cid = 1 THEN 1 ELSE 0 END),
            5
        ),
        '%'
    ) '數學及格率',
    SUM(
        CASE
        WHEN Cid = 3 THEN
            score
        ELSE
            0
        END
    ) / SUM(CASE WHEN Cid = 1 THEN 1 ELSE 0 END) AS '英語平均分',
    CONCAT(
        LEFT (
            100 * SUM(
                CASE
                WHEN Cid = 3
                AND score >= 60 THEN
                    1
                ELSE
                    0
                END
            ) / SUM(CASE WHEN Cid = 1 THEN 1 ELSE 0 END),
            5
        ),
        '%'
    ) '英語及格率'
FROM
    sc

 2一、查詢不一樣老師所教不一樣課程平均分從高到低顯示

需求:查詢老師,課程,分數(teacher,sc,course)

條件:不一樣老師,不一樣課程,平均分,倒序

說明:不一樣老師,按老師分組,不一樣課程,按課程分組,平均分 avg,倒序desc 老師爲條件,主要結果爲平均分,因此主表爲sc

SELECT
    teacher.Tname,
    course.Cname,
    AVG(sc.score)
FROM
    sc,
    course,
    teacher
WHERE
    sc.Cid = course.Cid
AND course.Tid = teacher.Tid
GROUP BY
    course.Tid,
    sc.Cid
ORDER BY
    AVG(sc.score) DESC;
SELECT
    MAX(z.Tname),
    MAX(c.Cname),
    AVG(t.score)
FROM
    sc t,
    course c,
    teacher z
WHERE
    t.Cid = c.Cid
AND c.Tid = z.Tid
GROUP BY
    c.Cid
ORDER BY
    AVG(t.score) DESC

2二、查詢以下課程成績第 3 名到第 6 名的學生成績單:語文(1),數學(2),英語 (3)[學生ID],[學生姓名],語文,數學,英語,平均成績

需求:(課程表,成績表,學生表)

條件:課程成績第 3 名到第 4名的學生成績單

說明:課程成績從第x名到第y名 order by 總成績,同一我的多列成績轉變爲一我的一列多課程成績,這樣的查詢能夠自關聯已課程iD爲條件,這裏avgScore 不能複用totalScore/4進行計算

SELECT DISTINCT
    sc.Sid,
    student.Sname,
    sc1.score AS '語文',
    sc2.score AS '數學',
    sc3.score AS '英語',
    sc4.score AS '政治',
    (
        IFNULL(sc1.score, 0) + IFNULL(sc2.score, 0) + IFNULL(sc3.score, 0) + IFNULL(sc4.score, 0)
    ) AS totalScore,
    (
        IFNULL(sc1.score, 0) + IFNULL(sc2.score, 0) + IFNULL(sc3.score, 0) + IFNULL(sc4.score, 0)
    )/4 AS avgScore
FROM
    student,
    sc
LEFT JOIN sc AS sc1 ON sc.Sid = sc1.Sid
AND sc1.Cid = 1
LEFT JOIN sc AS sc2 ON sc.Sid = sc2.Sid
AND sc2.Cid = 2
LEFT JOIN sc AS sc3 ON sc.Sid = sc3.Sid
AND sc3.Cid = 3
LEFT JOIN sc AS sc4 ON sc.Sid = sc4.Sid
AND sc4.Cid = 4
WHERE
    student.Sid = sc.Sid
ORDER BY
    totalScore
LIMIT 2,
 2;

2三、統計列印各科成績,各分數段人數:課程ID,課程名稱,[100-85],[85-70],[70-60],[ <60]

需求:課程信息,分數信息(課程表,分數表)

條件:各科成績

說明:統計if(條件) 個數等信息:case when else end

 

SELECT
    course.Cid,
    course.Cname,
    (
        SELECT
            SUM(
                CASE
                WHEN sc.score BETWEEN 85
                AND 100 THEN
                    1
                ELSE
                    0
                END
            )
        FROM
            sc
        WHERE
            sc.Cid = course.Cid
        AND sc.score
    ) AS '85-100',
    (
        SELECT
            SUM(
                CASE
                WHEN sc.score BETWEEN 70
                AND 85 THEN
                    1
                ELSE
                    0
                END
            )
        FROM
            sc
        WHERE
            sc.Cid = course.Cid
        AND sc.score
    ) AS '70-85',
    (
        SELECT
            SUM(
                CASE
                WHEN sc.score BETWEEN 60
                AND 70 THEN
                    1
                ELSE
                    0
                END
            )
        FROM
            sc
        WHERE
            sc.Cid = course.Cid
        AND sc.score
    ) AS '60-70',
    (
        SELECT
            SUM(
                CASE
                WHEN sc.score < 60 THEN
                    1
                ELSE
                    0
                END
            )
        FROM
            sc
        WHERE
            sc.Cid = course.Cid
        AND sc.score
    ) AS '<60'
FROM
    course;
SELECT
    course.Cid,
    course.Cname,
    SUM(
        CASE
        WHEN sc.score BETWEEN 85
        AND 100 THEN
            1
        ELSE
            0
        END
    ) AS '85-100',
    SUM(
        CASE
        WHEN sc.score BETWEEN 70
        AND 85 THEN
            1
        ELSE
            0
        END
    ) AS '70-85',
    SUM(
        CASE
        WHEN sc.score BETWEEN 60
        AND 70 THEN
            1
        ELSE
            0
        END
    ) AS '60-70',
    SUM(
        CASE
        WHEN sc.score < 60 THEN
            1
        ELSE
            0
        END
    ) AS '<60'
FROM
    course,
    sc
WHERE
    course.Cid = sc.Cid
GROUP BY
    course.Cid,
    course.Cname;

2四、查詢學平生均成績及其名次

需求:查詢平均成績和名次

條件:學生的平均成績和名次

說明:平均成績須要按學生分組,學生名次:子查詢中的分數和主查詢中的分數做比較統計個數 + 1 能夠做爲名次

 

SELECT
    Sid,
    avgScore,
    (
        SELECT
            1 + COUNT(avgScore)
        FROM
            (
                SELECT
                    Sid,
                    AVG(score) AS avgScore
                FROM
                    sc
                GROUP BY
                    Sid
            ) AS t1
        WHERE
            t1.avgScore > t2.avgScore
    ) AS ranking
FROM
    (
        SELECT
            Sid,
            AVG(score) AS avgScore
        FROM
            sc
        GROUP BY
            Sid
    ) AS t2
ORDER BY
    avgScore DESC

 2五、查詢各科成績前三名的記錄:(不考慮成績並列狀況)

需求:查詢課程表(sc)

條件:各科成績前三,不考慮並列

分析:並列是否考慮先不說,各科成績前三,說明兩個條件,各科,成績前三,須要先查詢前三的成績,參數爲課程id,由於mysql 不支持top,因此只能使用limit,又由於limit不能用在in裏邊,同時臨時表中不能使用主表字段作比較條件,這樣就給該查詢形成了困難。

這裏要用到一個函數EXISTS():exists對外表用loop逐條查詢,每次查詢都會查看exists的條件語句,當 exists裏的條件語句可以返回記錄行時(不管記錄行是的多少,只要能返回),條件就爲真,返回當前loop到的這條記錄,反之若是exists裏的條 件語句不能返回記錄行,則當前loop到的這條記錄被丟棄,exists的條件就像一個bool條件,當能返回結果集則爲true,不能返回結果集則爲 false

查詢sc表,已分數做爲條件在查詢sc表,若是子表分數大於主表分數的數量 > 1就是前一名,若是 > 2就是前2名,若是有並列都會查詢出來

 

SELECT
    *
FROM
    sc AS t
WHERE
    EXISTS (
        SELECT
            COUNT(1)
        FROM
            sc
        WHERE
            t.Cid = sc.Cid
        AND t.score < sc.score
        HAVING
            COUNT(1) < 1
    )
ORDER BY Cid

 

 26. 查詢每門課程被選修的學生數

需求:求學生數(sc表)

條件:每門課程被選修的學生數

說明:聚合函數,學生信息,按學生分組

SELECT
    sc.Cid,
    course.Cname,
    COUNT(sc.Sid)
FROM
    sc
JOIN course ON sc.Cid = course.Cid
GROUP BY
    sc.Cid;

2七、查詢出只選修了一門課程的所有學生的學號和姓名

需求:學生的學號和姓名(sc,student)

條件:只選修了一門課程

說明:sc,student兩個表關聯,最後有個條件一門課程,就是要對sc表中的Cid使用聚合函數,可是就不能再查下學生信息,這是個誤區。因此不考慮連接查詢,替換狀況就是查詢學生表經過in函數來查詢。經過查詢成績表中的學生id,按照學生id分組,一個學生id對應多個課程id,而後對課程id聚合數量做爲條件查出符合條件的學生id

 

SELECT
    student.Sid,
    student.Sname
FROM
    student
WHERE
    student.Sid IN (
        SELECT
            sc1.Sid
        FROM
            sc AS sc1
        GROUP BY
            sc1.Sid
        HAVING
            COUNT(Cid) = 1
    );

 

2八、查詢男生、女生人數

需求:查詢不一樣種類的學生人數(student)

條件:不一樣種類的學生人數

說明:同一表,同一查詢(不一樣條件)屢次查詢

 

SELECT
    COUNT(student.Ssex)
FROM
    student
GROUP BY
    student.Ssex
HAVING
    student.Ssex = 1;

SELECT
    COUNT(student.Ssex)
FROM
    student
GROUP BY
    student.Ssex
HAVING
    student.Ssex = 0;

 

2九、查詢姓「張」的學生名單

 

SELECT
    Sname
FROM
    student
WHERE
    Sname LIKE '%張';

 

30、查詢同名同性學生名單,並統計同名人數

說明:相同姓名,即要按照姓名分組。同名人數,即人數大於1

 

SELECT
    Sname,
    COUNT(Sname)
FROM
    student
GROUP BY
    Sname
HAVING
    COUNT(*) > 1;

 

3一、1981年出生的學生名單(注:Student表中Sage列的類型是datetime)

說明:日期轉換,date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s')   20080808222301

 

SELECT
    DATE_FORMAT(NOW(), '%Y') AS age,
    Sname
FROM
    student
WHERE
    DATE_FORMAT(NOW(), '%Y') = 2018;

 

3二、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列

說明:升序ASC,降序DESC

 

SELECT
	Cid,AVG(score)
FROM
	sc
GROUP BY
	Cid
ORDER BY
	AVG(score),Cid DESC

 

 

 

3三、查詢平均成績大於85的全部學生的學號、姓名和平均成績

說明:分組查詢,條件使用having

 

SELECT
    student.Sid,
    student.Sname,
    AVG(sc.score)
FROM
    student
INNER JOIN sc ON student.Sid = sc.Sid
GROUP BY student.Sid,student.Sname
HAVING
    AVG(sc.score) > 85

 

SELECT
    Sname,
    sc.Sid,
    AVG(score)
FROM
    student,
    sc
WHERE
    student.Sid = sc.Sid
GROUP BY
    sc.Sid,
    Sname
HAVING
    AVG(score) > 85;

 

 

 

3四、查詢課程名稱爲「數據庫」,且分數低於60的學生姓名和分數

 

SELECT
    student.Sname,
    IFNULL(sc.score,0)

FROM
    student,
    sc,
    course
WHERE
    student.Sid = sc.Sid
AND sc.Cid = course.Cid
AND sc.score < 60
AND course.Cname = "政治";
//這裏因爲sc表中score不能爲null,而且是全鏈接因此ifnull無心義


SELECT
    student.Sname,
    IFNULL(sc.score, 0)
FROM
    student
LEFT JOIN sc ON student.Sid = sc.Sid
LEFT JOIN course ON sc.Cid = course.Cid
WHERE
    sc.score < 60
AND course.Cname = "政治";
//這裏因爲student左連接其餘兩個表,因此ifnull給null值賦默認值

 

 

 

 

3五、查詢全部學生的選課狀況;

說明:若是爲null給默認值ifnull

 

SELECT
    sc.Sid,
    student.Sname,
    sc.Cid,
    course.Cname
FROM
    sc,
    student,
    course
WHERE
    sc.Sid = student.Sid
AND sc.Cid = course.Cid;

SELECT
    student.Sid,
    student.Sname,
    IFNULL(course.Cid,-1),
    IFNULL(course.Cname,"未選課程")

FROM student LEFT JOIN sc ON student.Sid = sc.Sid LEFT JOIN course ON sc.Cid = course.Cid 

 

 

 

 

3六、查詢任何一門課程成績在70分以上的姓名、課程名稱和分數;

說明:只要有一門課程在70分以上,沒有任何一門課程在70分以上,全部課程在70分以上

 

//只要有一門課程在70分以上-知足條件便可
SELECT DISTINCT student.Sid, student.Sname, sc.Cid, sc.score FROM student, sc WHERE student.Sid
= sc.Sid AND sc.score >= 70;

 

//全部課程在70分如下,多轉一,取反
SELECT DISTINCT
    student.Sid,
    student.Sname
FROM
    student
LEFT JOIN sc ON student.Sid = sc.Sid
WHERE
    student.Sid NOT IN (
        SELECT
            student.Sid
        FROM
            student
        LEFT JOIN sc ON student.Sid = sc.Sid
        WHERE
            sc.score >= 70
    )

 

 

 

//全部課程在70分以上,多轉一,取反
SELECT DISTINCT student.Sid, student.Sname FROM student LEFT JOIN sc ON student.Sid
= sc.Sid WHERE student.Sid NOT IN ( SELECT student.Sid FROM student LEFT JOIN sc ON student.Sid = sc.Sid WHERE sc.score < 70 )

 

A表左連接B表,以A表爲主,若是查詢字段有B表字段那麼A表中沒有關聯B表的關聯關係,那麼結果中就會出現一行A字段爲沒有,B字段爲null的列

 

 

3七、查詢不及格的課程,並按課程號從大到小排列

說明:倒序desc

 

SELECT sc.Cid FROM sc WHERE sc.score < 60 ORDER BY sc.Cid DESC

 

 

 

3八、查詢課程編號爲003且課程成績在80分以上的學生的學號和姓名;

 

SELECT
    sc.Sid,
    student.Sname
FROM
    student,
    sc
WHERE
    student.Sid = sc.Sid
AND sc.Cid = '1'
AND sc.score > 80

 

 

 

3九、求選了課程的學生人數

說明:多級查詢

 

SELECT COUNT(1) FROM (SELECT COUNT(sc.Sid) FROM sc GROUP BY sc.Sid) AS scount

 40、查詢選修「葉平」老師所授課程的學生中,成績最高的學生姓名及其成績

說明:成績最高,經過子查詢查詢出最高成績在和成績造成等於關係

 

SELECT
    sc.Sid,
    student.Sname,
    sc.score
FROM
    sc,
    student,
    course,
    teacher
WHERE
    sc.Sid = student.Sid
AND sc.Cid = course.Cid
AND course.Tid = teacher.Tid
AND teacher.Tname = "語文老師"
AND sc.score = (
    SELECT
        MAX(sc.score)
    FROM
        sc,
        teacher,
        course
    WHERE
        sc.Cid = course.Cid
    AND course.Tid = teacher.Tid
    AND teacher.Tname = "語文老師"
);

 

子查詢中可使用父查詢表中的字段,子查詢中查詢一個字段,不用給結果as別名

SELECT
    sc.Sid,
    student.Sname,
    sc.score
FROM
    sc,
    student,
    course,
    teacher
WHERE
    sc.Sid = student.Sid
AND sc.Cid = course.Cid
AND course.Tid = teacher.Tid
AND teacher.Tname = "語文老師"
AND sc.score = (
    SELECT
        MAX(sc1.score)
    FROM
        sc sc1
    WHERE
 sc1.Cid = sc.Cid
);

 4一、查詢各個課程及相應的選修人數

說明:按照課程id,課程名稱分組查詢選同一門課程的人數

 

SELECT
    course.Cid,
    course.Cname,
    COUNT(sc.Sid)
FROM
    sc
LEFT JOIN course ON sc.Cid = course.Cid
LEFT JOIN student ON sc.Sid = student.Sid
GROUP BY sc.Cid

 

SELECT COUNT(1) FROM sc GROUP BY Cid

4二、查詢不一樣課程成績相同的學生的學號、課程號、學生成績

說明:全部條件和查詢結果都在一張表中,能夠自關聯來知足多個條件

 

SELECT
    a.Sid,
    b.score
FROM
    sc AS a,
    sc AS b
WHERE
    a.score = b.score
AND a.Cid != b.Cid

 

4三、查詢每門課程成績最好的前兩名

說明:條件拆分:每門課程 + 成績前兩名,正常請求下若是說每門課程就要按照課程分組了,可是這裏的後續條件前兩名,就不能按照課程分組了,結果會是一門課程多條數據的一個集合。因此重點條件是第二個條件:前兩名,因此成績表裏的每一條數據和成績表裏的每一條數據做比較來求數量,條件爲課程id相同,且條件查詢分數大於主查詢分數,這個數量<=1就是前二名,由於若是至關於自關聯,這個數量的值爲0,那麼這門課程就一我的,若是這個數量是1,那麼說明同一門課比他分數高的有一人,那麼他就是第二名。固然,這個查詢是包含並列的。比我成績高有一我的,我倆就是前二名。

 

SELECT
    sc1.Sid,
    sc1.Cid,
    sc1.score
FROM
    sc AS sc1
WHERE
    score IN (
        SELECT
            sc2.score
        FROM
            sc AS sc2
        WHERE sc1.score = sc2.score
        ORDER BY sc2.score DESC
        LIMIT 2
        
    )

[Err] 1235 - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

 

包含並列成績
SELECT sc1.Sid, sc1.Cid, sc1.score FROM sc AS sc1 WHERE ( SELECT COUNT(
1) FROM sc AS sc2 WHERE sc2.Cid = sc1.Cid AND sc2.score > sc1.score ) <= 1 ORDER BY sc1.Cid

4四、統計每門課程的學生選修人數(超過10人的課程才統計)。要求輸出課程號和選修人數,查詢結果按人數降序排列,若人數相同,按課程號升序排列

 說明:在成績表中查詢每門課程輸出一行,典型的分組問題。

 

SELECT
    sc.Cid,
    COUNT(sc.Sid)
FROM
    sc
GROUP BY
    sc.Cid
HAVING
    COUNT(sc.Sid) > 2
ORDER BY
    COUNT(sc.Sid) DESC,
    sc.Cid ASC
SELECT
    sc.Cid,
    COUNT(1)
FROM
    sc
GROUP BY
    sc.Cid
HAVING
    COUNT(1) > 2
ORDER BY
    COUNT(1) DESC,
    sc.Cid ASC

4五、檢索至少選修兩門課程的學生學號

說明:至少選兩門課程,所選課程數大於1,查詢學號,按照學號分組,課程數大於1爲條件

 

SELECT
    sc.Sid
FROM
    sc
GROUP BY
    sc.Sid
HAVING
    COUNT(1) > 3

 

4六、查詢所有學生選修的課程的課程號和課程名

說明:所有學生id集合做爲條件查詢成績表

 

 

SELECT
    Cid,
    Cname
FROM
    course
WHERE
    Cid IN (SELECT Cid FROM sc GROUP BY Cid)

4七、查詢沒學過「葉平」老師講授的任一門課程的學生姓名

說明:沒有。。。 = not in 。。。

 

SELECT
    Sid,
    Sname
FROM
    student
WHERE
    Sid NOT IN (
        SELECT
            Sid
        FROM
            sc,
            course,
            teacher
        WHERE
            sc.Cid = course.Cid
        AND course.Tid = teacher.Tid
        AND teacher.Tname = "語文老師"
    )

 

4八、查詢兩門以上不及格課程的同窗的學號及其平均成績

說明:不及格成績>2的人

 

SELECT
    sc1.Sid,
    AVG(sc1.score)
FROM
    sc sc1
GROUP BY
    sc1.Sid
HAVING
    (
        SELECT
            count(1)
        FROM
            sc AS sc2
        WHERE
            sc1.Sid = sc2.Sid
        AND sc2.score < 60
    ) > 2;
SELECT
    Sid,
    AVG(score)
FROM
    sc
WHERE
    Sid IN (
        SELECT
            Sid
        FROM
            sc
        WHERE
            score < 60
        GROUP BY
            Sid
        HAVING
            COUNT(*) > 2
    )
GROUP BY Sid

4九、檢索「004」課程分數小於60,按分數降序排列的同窗學號

 

SELECT
    Sid
FROM
    sc
WHERE
    Cid = 2
AND score < 60
ORDER BY
    score DESC;

 

50、刪除「002」同窗的「001」課程的成績

 

DELETE
FROM
    sc
WHERE
    Sid = 1
AND Cid = 1
相關文章
相關標籤/搜索