多表操作

    自连接
        表的自身与自身做连接
        Select a.sname as 小类别名称,b.sname as 大类别名称
        from t_mc_type a,t_mc_type b where a.npid = b.nid
    内连接
        结果为两张表符合关联关系的结果集数据。不相等不在结果集中。

        解决不相等的数据办法:

        创建表,必须定义表之间的主外键关系。

        Select a.nid,a.sname,a.nmaxid,b.sname from
        t_mc a,t_mc_type b
        where a.nmaxid = b.nid and a.nid>10

        Select a.nid,a.sname,a.nmaxid,b.sname from
        t_mc a inner join t_mc_type b
        on a.nmaxid = b.nid where a.nid>10    

    外连接
        左外连接
            左外连接就是在等值连接的基础上加上主表中的未匹配数据
            以左表为基表,右表补空值。
            Select a.*,b.* from
            t_mc a,t_mc_type b
            where a.nmaxid = b.nid(+)

            Select a.*,b.* from t_mc a
            left outer join t_mc_type b
            on a.nmaxid = b.nid
        右外连接
            右外连接是在等值连接的基础上加上副表中的不匹配数据
            以右表为基表,左表补空值
            Select a.*,b.* from
            t_mc a,t_mc_type b
            where a.nmaxid(+) = b.nid

            Select a.*,b.* from t_mc a
            right outer join t_mc_type b
            on a.nmaxid = b.nid
        全连接

            Select a.*,b.* from t_mc a
            full outer join t_mc_type b
            on a.nmaxid = b.nid

    结果集连接

        union:并集,去掉重复
        union all:并集,不去掉重复
        intersect:交集
        minus:差集

    update student_grade s set s.grade =
    (select t.grade from student_makeup t
    where s.id=t.id) where s.id in(Select t.id from student_makeup t where s.id=t.id)

    update student_grade s set s.grade =
    (select t.grade from student_makeup t
    where s.id=t.id) where exists (Select t.id from student_makeup t where s.id=t.id)


聚合函数
    聚合函数一般和分组函数一起使用
    Count
        Select count(nid) from t_mc;
        Select count(1) from t_mc
        Select count(*) from t_mc;
    Max
    Min
    SUM
    AVG
        Select max(nid) from t_mc
        Select min(nid) from t_mc
        Select avg(nid) from t_mc
        Select sum(nid) from t_mc

    字符串函数
        Select simg,SUBSTR(simg,instr(simg,'.'),length(simg)) as 后缀后
        from t_mc where simg is not null

    转换
        to_char

        to_date

        Select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') from dual;

        Select to_date('2009-12-13 14:23:34','yyyy-MM-dd HH24:mi:ss') from dual;

        Select to_date(to_char(sysdate,'yyyy-MM-dd'),'yyyy-MM-dd') from dual;

    日期函数
        add_months:两个月份的操作
        Last_Day:获取月份的最后一天
        Months_Between:返回d1和d2之间月的数目
        Next_Day:给出日期date和星期x之后计算下一个星期的日期

        Select to_char(sysdate+30/1440,'yyyy-MM-dd HH24:mi:ss') from dual;
        Select to_char(sysdate+1+1/24+1/(24*60)) from dual;
        Select to_char(sysdate+1+1/24+1/(24*60),'yyyy-MM-dd HH24:mi:ss') from dual;
        Select trunc(Last_Day(sysdate)+1) from dual;

    错误函数
        SQLERRM
        SQLCODE
        declare
               var_i number;
        begin
               var_i := 10/0;
        exception
               when others then
                dbms_output.put_line('错误消息='||SQLERRM ||'错误代码='||SQLCode);                                     
        end;

    高级函数
        User:获取当前用户名
            Select user from dual;

        NVL:
            NVL(字符串,'返回值1');如果字符串为NULL值,返回返回值1,否则返回本身
            Select nvl(simg,'暂无图片') from t_mc;


    在SQL语句中做判断使用。
        DECODE:
            Select scode,decode(scode,
                        '350001','福州',
                        '350002','厦门',
                        '350003','泉州',
                        '350004','漳州',
                        '未知')
            from t_user;            
        CASE:
            Select smctag,
            case
                when smctag = '0' then '正常'
                when smctag = '1' then '缺货'
                when smctag = '2' then '正在发送'
                else '完成'
            end
            from t_mc order by smctag asc;

    将行转成列的功能配合聚合函数使用。
    Select year,sum(decode(month,'1',amount,'0')) as month1,
    sum(decode(month,'2',amount,'0')) as month2,
    sum(decode(month,'3',amount,'0')) as month3,
    sum(decode(month,'4',amount,'0')) as month4,
    sum(decode(month,'5',amount,'0')) as month5
     from table1 group by year;
    
    
     Select year,sum(case when month='1' then amount else 0 end) as month1,
    sum(case when month='2' then amount else 0 end) as month2,
    sum(case when month='3' then amount else 0 end) as month3,
    sum(case when month='4' then amount else 0 end) as month4,
    sum(case when month='5' then amount else 0 end) as month5
     from table1 group by year;


    Group by/having

        Group by:对数据进行分组。

        Select nmaxid,count(1) from t_mc

        group by nmaxid having count(1)>2

        Select * from(Select nmaxid,count(1) as temp_Count from t_mc

        group by nmaxid) t where t.temp_Count>2

    使用Group by的注意点

        分组查询有一个原则,就是 select
        后面的所有列中没有使用聚合函数的列,必须出现在group by后面

    每个部分,按性别的平均薪资

        Select a.dept_id,b.deptname_test,a.ssex,avg(a.salary) from t_user a,
        t_dept2 b where a.dept_id = b.deptid
        group by a.dept_id,a.ssex,b.deptname_test
        having avg(a.salary)>1000

    
        
    创建数据库脚本