1.sql语句基础
a)select * from tb_user t
b)update tb_user t set t.user_name ='hu284126748',t.age=1
c)insert into tb_user t(t.u_id,t.user_name,t.password,t.state) values(7,'huhuabin','asdasd','1')
d)delete from tb_user t where t.user_name='huhuabin'
2.GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组。
SELECT t.u_Id, COUNT(1) Cou FROM tb_user_course t GROUP BY t.u_id
3.NOT exists,则表示如果表B中没有记录能与表A这个记录连接,则表A的这个记录是符合条件的记录
SELECT u.u_name FROM TB_USER u WHERE NOT EXISTS
(SELECT * FROM TB_COURSE c WHERE NOT EXISTS
(SELECT * FROM TB_USER_COURSE uc
WHERE uc.u_id = u.u_id AND uc.c_id = c.c_id))
4.In 的用法
update TB_COURSE set TB_COURSE.C_NAME = '未知' where TB_COURSE.C_ID in
(select t.c_id from TB_USER_COURSE t where t.u_id = 4)
5. GROUP BY 复杂用法 及Nvl
SELECT t.u_Id,
t.Store_Id,
(SELECT Nvl(s.Uri, '')
FROM Tb_Store_Images s
WHERE s.u_Id = t.u_Id
AND Rownum = 1) Uri,
COUNT(1) Cou
FROM Tb_Store_Images t
WHERE t.Store_Id = 131039
AND t.Image_Type = '2'
GROUP BY t.u_Id, t.Store_Id
6.对于时间的一些查询
select * from tb_log t where t.create_date>=to_date('2012-4-1','yyyy-MM-dd') and t.create_date<=to_date('2012-4-30','yyyy-MM-dd')
a。上月末天:
SQL> select to_char(add_months(last_day(sysdate),-1),'yyyy-MM-dd') LastDay from
dual;
LASTDAY
----------
2005-05-31
b。上月今天
SQL> select to_char(add_months(sysdate,-1),'yyyy-MM-dd') PreToday from dual;
PRETODAY
----------
2005-05-21
c.上月首天
SQL> select to_char(add_months(last_day(sysdate)+1,-2),'yyyy-MM-dd') firstDay from dual;
FIRSTDAY
----------
2005-05-01
d按照每周进行统计
SQL> select to_char(sysdate,'ww') from dual group by to_char(sysdate,'ww');
TO
--
25
e。按照每月进行统计
SQL> select to_char(sysdate,'mm') from dual group by to_char(sysdate,'mm');
TO
--
06
f。按照每季度进行统计
SQL> select to_char(sysdate,'q') from dual group by to_char(sysdate,'q');
T
-
2
g。按照每年进行统计
SQL> select to_char(sysdate,'yyyy') from dual group by to_char(sysdate,'yyyy');
TO_C
----
2005
h.要找到某月中所有周五的具体日期
select to_char(t.d,'YY-MM-DD') from (
select trunc(sysdate, 'MM')+rownum-1 as d
from dba_objects
where rownum < 32) t
where to_char(t.d, 'MM') = to_char(sysdate, 'MM') --找出当前月份的周五的日期
and trim(to_char(t.d, 'Day')) = '星期五'
--------
03-05-02
03-05-09
03-05-16
03-05-23
03-05-30
如果把where to_char(t.d, 'MM') = to_char(sysdate, 'MM')改成sysdate-90,即为查找当前月份的前三个月中的每周五的日期。
i.oracle中时间运算
内容如下:
1、oracle支持对日期进行运算
2、日期运算时是以天为单位进行的
3、当需要以分秒等更小的单位算值时,按时间进制进行转换即可
4、进行时间进制转换时注意加括号,否则会出问题
SQL> alter session set nls_date_format='yyyy-mm-dd hh:mi:ss';
会话已更改。
SQL> set serverout on
SQL> declare
2 DateValue date;
3 begin
4 select sysdate into DateValue from dual;
5 dbms_output.put_line('源时间:'||to_char(DateValue));
6 dbms_output.put_line('源时间减1天:'||to_char(DateValue-1));
7 dbms_output.put_line('源时间减1天1小时:'||to_char(DateValue-1-1/24));
8 dbms_output.put_line('源时间减1天1小时1分:'||to_char(DateValue-1-1/24-1/(24*60)));
9 dbms_output.put_line('源时间减1天1小时1分1秒:'||
to_char(DateValue-1-1/24-1/(24*60)-1/(24*60*60)));