day41 mycql 函数

Posted 孟郊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了day41 mycql 函数相关的知识,希望对你有一定的参考价值。

 

一些经典的练习题,以及函数的简单用法,內建函数

技术分享图片
-- 函数
    python函数
    def fun1(a1,a2,a3):
        sum = a1+a2+a3
        return sum
    
    fun1(1,2,3)
    
    java中函数写法
    public String fun2(int i1,String s2){
    
        String sum = i1+s2;
        
        return sum;
    }
    A a = new A();
    a.fun2(1,"assda");
    
    javascript
    function fun3(a1,a2){
        var sum = a1+a2;
        return sum;
    }
    
    mysql 自定义函数
        create FUNCTION fun1(i int,x int)
            returns int -- 声明返回的类型
        BEGIN
            DECLARE sum int DEFAULT 0;
            SET  sum = i+x;
            return(sum);-- 提供返回结果
        end
        
        调用函数
            select 函数名称(参数值)
        在select使用函数
            select ren.*,fun1(ren.p_sal,100) from ren 
        博客表    
        id   name   time
        1     ADAS    2017-09-09
        2     sa第三    2017-10-09
        3     sadd    2017-10-19
        select DATE_FORMAT(now(),%Y年%m月)
        
        select CHAR_LENGTH("中午");
        select length("中午");

        select CONCAT("alex","asdsad","在法国风格",null)

        select CURDATE();
        select CURTIME();
        select now();

        SQL练习:
        --  1、查询课程编号“001”比课程编号“002”  成绩高的所有学生的学号;

        select s_score from score where c_id =1;

        select s_score from score where c_id =2;


        select A.s_id from (select s_score,s_id from score where c_id =1) as A,
            (select s_score,s_id from score where c_id =2) B where A.s_id = B.s_id 
        and A.s_score > B.s_score;
        
        --  2、查询平均成绩大于60分的同学的学号和平均成绩;

        select avg(s_score),s_id from score where s_id = 1  GROUP BY s_id having avg(s_score)>60
        
        -- 3、查询所有同学的学号、姓名、选课数、总成绩;

        select student.s_id,student.s_name,count(1) as选课数,sum(s_score) from score,student where score.s_id = student.s_id GROUP BY s_id
        -- 4查询含有""的老师的个数;

        select count(t_id) from teacher where t_name like %子%
    
        -- 5、查询没学过“老子”老师课的同学的学号、姓名;

        select c.c_id from teacher t,course c where t.t_id = c.t_id  and t.t_name =老子;

        select s_id from score s where s.c_id in
        (select c.c_id from teacher t,course c where t.t_id = c.t_id  and t.t_name =老子);

        select DISTINCT student.s_id,student.s_name from score,student where score.s_id not in (select s_id from score s where s.c_id in
        (select c.c_id from teacher t,course c where t.t_id = c.t_id  and t.t_name =老子)
        )
        and student.s_id = score.s_id 
        
        -- 6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

        select st.s_id,st.s_name from score s 
            inner join student st on s.s_id = st.s_id
         where s.c_id = 1 or s.c_id =2
        GROUP BY s.s_id HAVING count(s.s_id) = 2;

        select st.s_id,st.s_name from (select * from  score s where s.c_id = 1 union all select * from  score s where s.c_id = 2 ) as lin
            inner join student st on lin.s_id = st.s_id
        GROUP BY lin.s_id HAVING count(lin.s_id) = 2
        
        
        注意: union :表示去重复合并表
               union ALL :表示不去重复合并表
        
        
        --  7、查询学过“老子”老师所教的所有课的同学的学号、姓名;
        #1.先查询"老子"老师教哪些课程
        #2.再查询哪些学生学习了这些课程
        #3.再根据学生编号分组,如果分组后的个数 ="老子"老师所教授课程的个数,
        # 则表示学过该老师所有课程.

        select c_id from course c ,teacher t where c.t_id = t.t_id and t.t_name=张雪峰;

        select st.s_id,st.s_name from score,student st where score.s_id = st.s_id and c_id in(
        select c_id from course c ,teacher t where c.t_id = t.t_id and t.t_name=张雪峰)
        GROUP BY s_id having count(sc_id) = 
         (select count(1) from course c ,teacher t where c.t_id = t.t_id and t.t_name=张雪峰)
        
        
        --  8、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;
        select a.s_id from 
                (select * from score where c_id =1) a, 
                (select * from score where c_id =2) b 
        where a.s_id = b.s_id and a.s_score < b.s_score;   
        
        
        -- 9、查询有课程成绩小于60分的同学的学号、姓名; 

        select DISTINCT st.s_id,st.s_name from student st,score sc where st.s_id = sc.s_id and sc.s_score < 60
        
        
        --  10、查询没有学全所有课的同学;

        select count(1) from course c

        select * from student st where s_id in (
        select s_id from score sc GROUP BY sc.s_id HAVING count(1)
         <> (select count(1) from course c)
        )
        
        -- 11、查询至少有一门课与学号为“002”的同学所学相同的同学的学号和姓名;

        select sc.c_id from score sc,student st where sc.s_id =st.s_id and sc.s_id =2

        select DISTINCT st.s_id,st.s_name  from score sc,student st where 
        st.s_id =sc.s_id and sc.c_id in(select sc.c_id from score sc where sc.s_id =2)
        
        -- 12、查询学过 学号为“002”同学全部课程 的其他同学的学号和姓名;

        select sc.c_id from score sc where sc.s_id = 2

        select st.s_id,st.s_name from score sc,student st where sc.s_id =st.s_id and st.s_id !=2 and sc.c_id in
            (select sc.c_id from score sc where sc.s_id = 2) 
        GROUP BY sc.s_id HAVING count(1) = (select count(1) from score sc where sc.s_id = 2)


        
        -- 14、把“score”表中“老子”老师教的课的成绩都更改为此课程的平均成绩;
        UPDATE score set s_score = (select * from (select avg(sc.s_score) from teacher t,course c,score sc where c.t_id = t.t_id 
            and sc.c_id = c.c_id and t.t_name=张雪峰 ) ss)
         
        where score.sc_id  in( select * from 
        (select sc_id from teacher t,course c,score sc where c.t_id = t.t_id 
            and sc.c_id = c.c_id and t.t_name=张雪峰) aaa )
    
View Code

这里是mycql里面的内建函数里面时间的一些符号的使用方法

 https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html

 

函数的一些需要掌握的知识点(较常用的):

技术分享图片
-- create function f(w int,x int )
--     returns int 
-- begin 
--     declare sum int default 0;
--     set sum=w+x;
--     return(sum);
-- end
-- 
-- select person.*,f(person.p_sal,200)from person 

-- select DATE_FORMAT(now(),%Y%M%W)
-- select CHAR_LENGTH(午夜)
-- select LENGTH(午夜)
-- select CONCAT(alex,sowhat,午夜,NULL)  如果这里有null,就会直接显示空
-- select date_format (2001-10-08 20:00:30,%W %M %Y);
-- select DATE_FORMAT(2007-10-08 20:00:00,%H:%i:%s)  -- h=hour,i=minute,s=SECOND
-- select date_format(2007-10-08 20:00:00,%a %b %j)-- a是首字母大写的星期,b是首字母大写的月份 j是day of year,一年中的第几天
-- select DATE_FORMAT(1997-10-04 22:23:05,%H %k %I %r %T %S %w %s) -- H是小时,显示的小时;K也是小时,是一天中第几个小时(23小时制);I是一天中的第几个小时(12小时制);r是12小时制,显示当前时间的时分秒,有后缀AM或者PM;T是24小时制显示时分秒;S是秒,第几秒;
-- w是一个星期中的第几天,mon是第一天,Saturday是第六天
-- select date_format(1999-01-01,%X %V) -- 哪一年中的第几个礼拜 这一天是第1998年的第52个礼拜
-- select date_format(2006-06-10,%d)  -- d是一个月中的第几天
View Code

 

以上是关于day41 mycql 函数的主要内容,如果未能解决你的问题,请参考以下文章

mycql 数据库之约束

apriori片段代码

day 41 mysql 函数 事物

如何从片段 KOTLIN 中调用意图 [重复]

Oracle LASTu DAY函数

day06-jsp