• sql积累


    • mysql 修改一列自增长

    set @rownum=0;

    update a

    SET id = (

    select @rownum := @rownum +1 as nid)

    WHERE id < 10;

    l  mysql 查询重复数据及其条数

    select login_name,count(*) as count from sys_user group by login_name having count>1;

    • mysql 查询一个表中没有存在在另一个表的数据

    SELECT * FROM A  WHERE  id  NOT  IN  ( SELECT id FROM B);或者SELECT * FROM A  WHERE      NOT  EXISTS  (          SELECT 1          FROM B          WHERE B.id = A.id );或者SELECT    A.*  FROM    A  LEFT JOIN B     ON (A.id = B.id) WHERE   b.id  IS  NULL

     

    1.添加PRIMARY KEY(主键索引) 

    mysql>ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` ) 

    2.添加UNIQUE(唯一索引) 

    mysql>ALTER TABLE `table_name` ADD UNIQUE ( 

    `column` 

    3.添加INDEX(普通索引) 

    mysql>ALTER TABLE `table_name` ADD INDEX index_name ( `column` ) 

    4.添加FULLTEXT(全文索引) 

    mysql>ALTER TABLE `table_name` ADD FULLTEXT ( `column`) 

    5.添加多列索引 

    mysql>ALTER TABLE `table_name` ADD INDEX index_name ( `column1`, `column2`, `column3` )

    • 选择查询样例

    <choose>

    <when test="1==type">

    <if test="0==chilId">

    select id  resourceId,cmdb_app_id cmdbId,cmdb_app_name cmdbApplicationName,third_id chilId,type,

    third_name otherApplicationName, create_time createTime from third_cmdb_mapping where type = #{type}

    <if test=" null!= cmdbApplicationName and '' !=cmdbApplicationName">

                                       and cmdb_app_name   like CONCAT('%',trim('${cmdbApplicationName}'),'%')

    </if>

    <if test=" null!= otherApplicationName and '' !=otherApplicationName">

                                       and third_name   like CONCAT('%',trim('${otherApplicationName}'),'%')

    </if>

                          

    </if>

    l  <if test="1==chilId">

    SELECT A.resource_id resourceId,A.pro_value cmdbApplicationName FROM  cmdb_resource A

    WHERE A.pro_key = 'CINAME' and A.resource_type = 'APPLICATION' and A.resource_id  NOT  IN  ( SELECT B.cmdb_app_id FROM third_cmdb_mapping  B where type =#{type})

    <if test=" null!= cmdbApplicationName and '' !=cmdbApplicationName">

                                       and  pro_value   like CONCAT('%',trim('${cmdbApplicationName}'),'%')

    </if>

    <if test=" null!= otherApplicationName and '' !=otherApplicationName">

                                       and 1=2

    </if>

                                 

    </if>

    </when>

    </choose>

    • 查询时给某列赋值

    select u.id,u.name, 

     (case u.sex    

     when 1 then '男' 

      when 2 then '女'  

     else '空的'  

     end     )性别  

     from users u;

    •  case 第二种用法 判断  条件在when开始写
    • select ENAME,job,(case
      when sal<500 then '低级员工'
      when 500<sal and sal<1000 then '中级员工'
      when sal >1000 then '高级员工'
      else ''       else 可以不写
      end) jibei
      from emp;

    • mysql 模糊查询 去除空格

    <if test=" null!= cmdbApplicationName and '' !=cmdbApplicationName">

    and cmdb_app_name   like CONCAT('%',trim('${cmdbApplicationName}'),'%')

    </if>

     

     

    练习sql

    1、 查询Student表中的所有记录的Sname、Ssex和Class列。

    select s.sname name,s.ssex sex,s.class class from students s;

    2、 查询教师所有的单位即不重复的Depart列。

    select DISTINCT (t.depart) depart from teachers t;

    3、 查询Student表的所有记录。

    select * from students;

    4、 查询Score表中成绩在60到80之间的所有记录。

    select * from scores where degree BETWEEN 60 and 80;

    5、 查询Score表中成绩为85,86或88的记录。

    select * from scores where degree in ('85','86','88');

    6、 查询Student表中“95031”班或性别为“女”的同学记录。

    select * from students s where s.class = '95031' or s.ssex ='女'

    7、 以Class降序查询Student表的所有记录。

    select * from students ORDER BY class desc;

    8、 以Cno升序、Degree降序查询Score表的所有记录。

    select * from scores s ORDER BY s.cno asc,s.degree desc;

    9、 查询“95031”班的学生人数。

    select COUNT(1) from students s where s.class = '95031'

    10、查询Score表中的最高分的学生学号和课程号。

    select s.cno cno,s.sno sno from scores s ORDER BY s.degree desc LIMIT 1;

    11、查询‘3-105’号课程的平均分。

    select s.cno, Avg(s.degree) avg

    from scores s ORDER BY s.cno and s.cno = '3-105';

    12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

    select s.sno,AVG(degree) from scores s WHERE s.cno like '3%' group by s.cno HAVING COUNT(s.sno) >= 5;

    13、查询最低分大于70,最高分小于90的Sno列。

    SELECT Sno

    FROM Scores

    GROUP BY Sno

    HAVING MAX(Degree)<90 AND MIN(Degree)>70;

    14、查询所有学生的Sname、Cno和Degree列。

    select s.sname,c.cno,c.degree from students s inner join scores c on s.sno = c.sno ORDER BY s.sname

    15、查询所有学生的Sno、Cname和Degree列。

    select a.sno,b.degree,c.cname from test.students a,test.scores b,test.courses c where a.sno = b.sno and c.cno = b.cno;

    16、查询所有学生的Sname、Cname和Degree列。

    select a.sname,b.degree,c.cname from test.students a,test.scores b,test.courses c where a.sno = b.sno and c.cno = b.cno;

    17、查询“95033”班所选课程的平均分。

    SELECT Cname,AVG(Degree)

    FROM Students INNER JOIN Scores

    ON(Students.Sno=Scores.Sno) INNER JOIN Courses

    ON(Scores.Cno=Courses.Cno)

    WHERE Class='95033'

    GROUP BY Courses.Cno

    ORDER BY Cname;

    18、假设使用如下命令建立了一个grade表:

    create table grade(low   number(3,0),upp   number(3),rank   char(1));

    insert into grade values(90,100,’A’);

    insert into grade values(80,89,’B’);

    insert into grade values(70,79,’C’);

    insert into grade values(60,69,’D’);

    insert into grade values(0,59,’E’);

    commit;

    现查询所有同学的Sno、Cno和rank列。

    SELECT Sno,Cno,rank

    FROM Scores INNER JOIN grade

    ON(Scores.Degree>=grade.low AND Scores.Degree<=grade.upp)

    ORDER BY Sno;

    19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。

    --标准答案

    SELECT s1.Sno,s1.Degree

    FROM Scores AS s1 INNER JOIN Scores AS s2

    ON(s1.Cno=s2.Cno AND s1.Degree>s2.Degree)

    WHERE s1.Cno='3-105' AND s2.Sno='109'

    ORDER BY s1.Sno;

    --自己写的垃圾sql

    SELECT * FROM students  inner join scores on students.sno = scores.sno  inner join courses on courses.cno = scores.cno where courses.cno = '3-105' and  scores.degree > ( select degree from students inner join scores on students.sno =scores.sno inner join courses on courses.cno = scores.cno where students.sno='109' and courses.cno = '3-105') order by students.sno;

    20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。

    SELECT *

    FROM Scores

    GROUP BY Sno

    HAVING COUNT(cno)>1 AND Degree!=MAX(Degree);

    21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。

    --标准答案

    SELECT s1.Sno,s1.Degree

    FROM Scores AS s1 INNER JOIN Scores AS s2

    ON(s1.Cno=s2.Cno AND s1.Degree>s2.Degree)

    WHERE s1.Cno='3-105' AND s2.Sno='109'

    ORDER BY s1.Sno;

    --垃圾sql

    select * from  scores a where a.degree>(

    select b.degree from scores  b where b.sno ='109' and b.cno = '3-105'

    ) order by a.sno

    22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。

     select s1.sno,s1.sname,s1.sbirthday from students s1  inner join students s2

     on (YEAR(s1.sbirthday) = YEAR(s2.sbirthday)) where s1.sno='108'

    23、查询“张旭“教师任课的学生成绩。

    SELECT Sno,Degree

    FROM Scores INNER JOIN Courses

    ON(Scores.Cno=Courses.Cno) INNER JOIN Teachers

    ON(Courses.Tno=Teachers.Tno)

    WHERE Teachers.Tname='张旭';

    24、查询选修某课程的同学人数多于5人的教师姓名。

    --自己写的还算凑合

     select t.tname from courses c

     inner join teachers t

     on c.tno = t.tno

     inner join scores e

     on e.cno = c.cno

     inner join students s

     on s.sno = e.sno

     group by c.cno

     HAVING COUNT(s.sno)>=5;

    --标准答案

     SELECT DISTINCT Tname

    FROM Scores INNER JOIN Courses

    ON(Scores.Cno=Courses.Cno) INNER JOIN Teachers

    ON(Courses.Tno=Teachers.Tno)

    WHERE Courses.Cno IN(SELECT Cno FROM Scores GROUP BY(Cno) HAVING COUNT(Sno)>5);

    25、查询95033班和95031班全体学生的记录。

    SELECT *

    FROM Students

    WHERE Class IN ('95033','95031')

    ORDER BY Class;

    26、查询存在有85分以上成绩的课程Cno.

    SELECT DISTINCT Cno

    FROM Scores

    WHERE Degree>85;

    27、查询出“计算机系“教师所教课程的成绩表。

    select c.degree from courses a

    inner join teachers b

    on a.tno = b.tno

    inner join scores c

    on c.cno = a.cno

    where b.depart = '计算机系'

    28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。

    --标准答案

    SELECT Tname,Prof

    FROM Teachers

    WHERE Depart='计算机系' AND Prof NOT IN(

        SELECT DISTINCT Prof

        FROM Teachers

        WHERE Depart='电子工程系');

    --垃圾答案

    select DISTINCT(prof) from teachers

    where depart= '计算机系' or depart= '电子工程系'

    GROUP BY prof

    29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。

    SELECT Cno,Sno,Degree

    FROM Scores

    WHERE Cno='3-105' AND Degree > ANY(

        SELECT Degree

        FROM Scores

        WHERE Cno='3-245')

    ORDER BY Degree DESC;

     

    30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.

    SELECT Cno,Sno,Degree

    FROM Scores

    WHERE Cno='3-105' AND Degree > ALL(

        SELECT Degree

        FROM Scores

        WHERE Cno='3-245')

    ORDER BY Degree DESC;

    31、查询所有教师和同学的name、sex和birthday.

    SELECT Sname,Ssex,Sbirthday         FROM Students         UNION         SELECT Tname,Tsex,Tbirthday         FROM Teachers;

    32、查询所有“女”教师和“女”同学的name、sex和birthday.

    SELECT Sname,Ssex,Sbirthday        FROM Students  WHERE Ssex='女'  UNION  SELECT Tname,Tsex,Tbirthday  FROM Teachers  WHERE Tsex='女';

    33、查询成绩比该课程平均成绩低的同学的成绩表。

    SELECT s1.*

    FROM Scores AS s1 INNER JOIN (

        SELECT Cno,AVG(Degree) AS aDegree

        FROM Scores

        GROUP BY Cno) s2

    ON(s1.Cno=s2.Cno AND s1.Degree<s2.aDegree);

    34、查询所有任课教师的Tname和Depart.

    SELECT Tname,Depart  FROM Teachers  WHERE Tno IN(      SELECT Tno      FROM Courses  );

    35  查询所有未讲课的教师的Tname和Depart.

    SELECT Tname,Depart  FROM Teachers  WHERE Tno NOT IN(      SELECT Tno     FROM Courses  );

    36、查询至少有2名男生的班号。

    -- 自己代码 都可以

    select * from students GROUP BY class HAVING COUNT(ssex) > 2

    --提供sql

    SELECT Class,COUNT(1) AS boyCount

    FROM Students

    WHERE Ssex='男'

    GROUP BY Class

    HAVING boyCount>=2;

    37、查询Student表中不姓“王”的同学记录。

    SELECT * FROM Students WHERE Sname NOT LIKE '王%';

    38、查询Student表中每个学生的姓名和年龄。

    select YEAR(NOW()) -YEAR(sbirthday)  from students

    39、查询Student表中最大和最小的Sbirthday日期值。

    SELECT MIN(Sbirthday),MAX(Sbirthday) FROM Students;

    40、以班号和年龄从大到小的顺序查询Student表中的全部记录。

    SELECT *  FROM Students  ORDER BY Class DESC,Sbirthday ASC;

    41、查询“男”教师及其所上的课程。

    select * from teachers inner join courses on teachers.tno = courses.tno where teachers.tsex = '男'

    42、查询最高分同学的Sno、Cno和Degree列。

    SELECT *

    FROM Scores

    GROUP BY Cno

    HAVING Degree=Max(Degree);

    43、查询和“李军”同性别的所有同学的Sname.

    SELECT s1.Sname

    FROM Students AS s1 INNER JOIN Students AS s2

    ON(s1.Ssex=s2.Ssex)

    WHERE s2.Sname='李军';

    44、查询和“李军”同性别并同班的同学Sname.

    SELECT s1.Sname

    FROM Students AS s1 INNER JOIN Students AS s2

    ON(s1.Ssex=s2.Ssex and s1.class=s2.class)

    WHERE s2.Sname='李军';

    45、查询所有选修“计算机导论”课程的“男”同学的成绩表

    --自己写的ok代码

    select * from courses a inner join scores b

    on a .cno = b.cno inner join students  c

    on b.sno= c.sno

    WHERE a.cname = '计算机导论' and c.ssex = '男';

    --提供答案

    SELECT *

    FROM Scores

    WHERE Sno IN (

        SELECT Sno

        FROM Students

        WHERE Ssex='男') AND

        Cno IN (

        SELECT Cno

        FROM Courses

        WHERE Cname='计算机导论');

  • 相关阅读:
    利用matplotlib进行数据可视化
    《操作系统》课程笔记(Ch11-文件系统实现)
    《操作系统》课程笔记(Ch10-文件系统)
    《数据库原理》课程笔记 (Ch06-查询处理和优化)
    《计算机网络》课程笔记 (Ch05-网络层:控制平面)
    《计算机网络》课程笔记 (Ch04-网络层:数据平面)
    《计算机网络》课程笔记 (Ch03-运输层)
    东南大学《软件测试》课程复习笔记
    《数据库原理》课程笔记 (Ch05-数据库存储结构)
    《操作系统》课程笔记(Ch09-虚拟内存)
  • 原文地址:https://www.cnblogs.com/studynode/p/9881693.html
Copyright © 2020-2023  润新知