数据库基本操作语句介绍

常用表名
flysysdictionary(字典表)、flysysinfo(密码表)、flycardinfo(卡户表)、flyemployee(人事表)、flycgdetail(消费流水表)、flycompanys(公司表)、flyattrecord(考勤流水表)、flycgsublist(补贴信息表)、view视图   、flyattempleaveinfo(考勤请假信息表)  、flysysprocess(事件表)、flysysuser(登录用户表)、flyemppost(职位表)

常用条件名  
empperno(平台工号)、empno工号 、companyid企业编号、empcardno卡号、empid员工编号(库里是唯一的)、empcardid(卡应用序列号,对应卡7扇区0571的后八位数字)


常用语句
查询语句:select * from 表名 where 唯一条件
修改语句:update 表名 set 修改内容 where 唯一条件
删除语句:delete  表名 where 唯一条件 
增加语句:insert into flycgdetail(,,...)values(,,...)
统计语句:select count(*)from
模糊查询:where 字段名 like  '%XXXXX%' 
清除表数据:truncate  table  表名 
在操作前,最好先备份好数据库和表。字符引号必须是英文下的字符,* 号是查询语句中代表所询所有字段值。在删除或者修改数据时,修改或者删除语句所带条件必须是唯一的,所以必须先查询确定出数据后再用修改或删除语句。独立版操作跟平台有差别,需要特别注意。

字符串类型的数据要加引号, 数字类型的不用加。 例如:empname='员工1' 和 empno=3
   
要同时满足两个条件时,可以用and,只需满足其中一个时用or。例如:select * from flyemployee where empname='员工1' and  empno=3

多个字段同时修改时内容直接用 "," 而不用and,不能用于where后。 例如:update flycgdetail set money=850,money=950 where empcardno=582599548 and listid=5895

在一段范围条件时,可以用between。例如:select * from flycgdetail where listdate between '2016-01-03 00:00:00'and '2016-01-07 23:59:59'

for update可用于语句后,如此执行全部语句后可进行手动修改,一般用于修改无规则少量数据。例如:select * from flyemployee where empname='员工1' and  empno=3  for updat

在语句前加--可以屏蔽该段语句 例如:-- select * from flyemployee where empname='员工1' and  empno=3

网页版可用名表(表后面直接加上公司ID)进行查询,减少条件。适用于因数据量大、各企业分开的表,比如消费表、考勤表等。例如:select * from flycgdetail61  where empcardno='3720744248' order by listdate desc

按姓名查询条件如果用empname=‘’查询不出来时,用 empname like '%姓名%'

语句后加上order by empid desc 是按 empid 的降序排,升序是加关键字 asc 、降序是加 desc 。例如:select * from flyemployee order by empid desc

数据按照时间进行排序,日期用‘/’或‘-’隔开,如果数据库日期字段的类型是 date 就要用to_date()转换。如果数据库日期字段的类型是varchar2/char(字符串)就用to_char。
第一种
select * from flycmdinfo61 where companyid=61 and cmdsendtime <to_date('2016-08-31 23:59:59','yyyy-mm-dd hh24:mi:ss') order by confirmdate asc
第二种
select * from flycmdinfo61 where companyid=61 and cmdsendtime <to_char('2016-08-31 23:59:59','yyyy-mm-dd hh24:mi:ss') order by confirmdate asc
对时间要求不高时也可用
select * from flycmdinfo61 where companyid=61 and cmdsendtime <to_date('2016/07/06','yyyy/mm/dd') order by confirmdate asc

独立版

独立版三餐统计不显示的调整语句:
select * into 新表名  from 表名 where 条件范围 
sp_rename  ‘新表名’,‘表名’
sp_rename  ‘新表名’,‘表名’

select * into flycgdetail123 from  flycgdetail where  需要保留的时间范围

sp_rename 'flycgdetail','flycgdetail0410'        

sp_rename 'flycgdetail123','flycgdetail'

原来所有数据现在都在flycgdetail0410中     flycgdetail123中是你要保留的数据 ,现在改名成flycgdetail

select to_char(listdate,'yyyy-mm') listdate ,count(listmoney),sum(listmoney/100) from flycgdetail61 
where listtype = '2101' 
and listdate >= to_date('2014-01-01 00:00:00','yyyy-mm-dd hh24:mi:ss') 
and listdate <= to_date('2014-12-20 23:59:59','yyyy-mm-dd hh24:mi:ss') group by to_char(listdate,'yyyy-mm') 

delete flyotherdevice t where devsn between '0020100000000000000101' and '0020100000000000000156'


独立版查询、删除一段时间消费记录语句示例:
先查询
select * into flycgdetail20170527  from flycgdetail 

select * from flycgdetail where listdate >'2017-01-01 00:00:00' and listdate <'2017-05-26 23:59:59' and listtype=1 or  listtype=200 order by listdate

独立版备份表
select * into flycgdetail20170527  from flycgdetail


独立版删除卡户信息
delete from flycardinfo

根据查询语句创建表
select * into 新表表名 from 旧表表名 where 条件
修改表名
EXEC sp_rename ‘[原有表名]’, '[新表名]'


批量充值记录与卡号绑定更新
update flycgdetail  set flycgdetail.empno=a.empno,flycgdetail.deptid=a.deptid,flycgdetail.empcardid=a.empcardid from 
(select a.empno,a.empcardid,a.empcardno,b.deptid from flycardinfo a inner join flyemployee b on a.empno=b.empno
) a where flycgdetail.empno=-1 and a.empcardno=flycgdetail.empcardno

独立版分条统计充值和消费语句
select empno,sum(isnull (cz,0))/100 as cz,sum(isnull (xf,0))/100 as xf from(
select empno,sum(case when listtype=101 then listmoney end) as cz,
       sum(case when listtype=1 then listmoney end) as xf
from flycgdetail where listdate>'2017-05-01 00:00:00' and
listdate<'2017-05-31 23:59:59' group by empno
 ) a  group by empno order by empno


独立版统计一段时间充值和消费总额
select sum(isnull (cz,0))/100 as cz,sum(isnull (xf,0))/100 as xf from(
select empno,sum(case when listtype=101 then listmoney end) as cz,
       sum(case when listtype=1 then listmoney end) as xf
from flycgdetail where listdate>'2017-05-01 00:00:00' and
listdate<'2017-05-31 23:59:59' group by empno
 ) a


独立版全部消费数据按设备统计
select sum(ListMoney)/100.00 as XF,sum(wltype)/1 as CS,devsn from flycgdetail 
where listdate between '2017-05-01 00:00:00' and '2017-05-31 23:59:59' and listtype in(1,200) group by devsn

充值按操作员汇总语句
select sum(ListMoney)/100.00 as CZ,operatorcode from flycgdetail 
where listdate between '2017-05-01 00:00:00' and '2017-05-31 23:59:59' and listtype=102 group by operatorcode

查询修改充值信息
select * from flycgdetail where listdate between '2017-05-01 00:00:00' and '2017-05-31 23:59:59' and operatorcode='huxinliang' and listtype=101 order by listdate desc

select * from flycgdetail where cdid=1686069 and operatorcode='huxinliang' and listtype=101

flycgdetail set ListMoney=60000,cardmoney=60210 where cdid=1686069 and operatorcode='huxinliang' and listtype=101


消费流水表增加充值语句
insert into flycgdetail(empno,empcardno,deptid,listmoney,cardmoney,listtype,listdate,timeid,listid,devno,subid,edttime,operatorcode,empcardid,wltype) 
values(95,'4035061317',406,10000,15295,101,
'2017-06-24 09:01:11',0,0,0,0,'2017-06-24 09:01:11','huxinliang',15347,1)

卡户操作表增加充值语句
insert into flycodetail(empid,empno,empcardno,cokind,cocontent,edttime,operatecode) values
(95,95,4035061317,'充值','充值 100.00','2017-06-24 09:01:11','admin')

修改卡户操作表的信息
update flycodetail set empid=95 where coid=51049 and empno=95

系统日志表查询语句
select * from  flysyslog where  eventuser='admin'order by eventdate desc

系统日志表增加充值语句
insert into flysyslog(eventtype,eventname,eventdate,eventuser,deptcode) values
('账户中心','充值,卡号:4035061317,工号:DF0094,金额:10000 分','2017-06-24 09:01:11','admin','01')

卡户明细表增加充值语句
insert into flycodetail(empno,empcardno,cokind,cocontent,edttime,operatecode) values
(95,4035061317,'充值','充值 100.00','2017-06-24 09:01:11','admin')

网页版语句

1版平台修改用户登录密码(这个是要用MD5加密的,将USERPWD数据修改成4645441a535a(代表密码123456)。
select * from flysysuser  where companyid=157 for update

卡库中删除卡号示例;
select * from crm_cardstore where rfid='331302673' for update   (查询出该卡号后,手动点击解锁按钮,选中内容后点击(-),将331302673从卡库删除,再点击旁边的勾,最后点击提交事务按钮)

模糊查询
select * from flycompanys where companyname like'%红河州%'

网页版备份表
create table flycgdetail95_21080111 as  select * from flycgdetail95 

字段自动查询
select * from flycgdetail91 a where a.

增加语句
insert into flycgdetail157(cdid,empcardno,empcardid,listmoney,cardmoney,listtype,listdate,listid,
devno,subid,devsn,poslistid,wltype) values(cgdetailid.nextval,'1670588907',00455855,800,3150,2101,
to_date('2017-05-22 12:10:30','yyyy-mm-dd hh24:mi:ss'),217,1,0,'0025712015043000000007',9652,2)

修改人员语句示例:
第一种
select * from flyemployee where empno='JM20140003'and companyid=61       (先在人事表中查询出企业编号为61且工号为JM20140003的人员,看是否唯一)
update flyemployee set  empno='GF20140152' where empno='JM20140003'and companyid=61     (将人事表中企业编号为61且工号为JM20140003的人员工号修改为GF20140152)
第二种
select * from flyemployee where empno='JM20140003'and companyid=61 for update
手动点击解锁按钮,选中内容后进行修改或者删除,最后点击提交事务按钮。

修改企业密钥语句示例:
update flysysinfo set m1key='111111111111;222222222222;' where companyid=61         

消费记录排序语句示例:
select * from flycgdetail61  where empid=4858  order by listdate desc  (在企业编码为61的消费流水表里对empid=4858的员工消费流水进行降序排列)

select * from flycgdetail61  where empid=4858 and listdate >= to_date('2016-12-5 00:00:00','yyyy-mm-dd hh24:mi:ss') and  listdate <= to_date('2016-12-14 23:59:59','yyyy-mm-dd hh24:mi:ss')  order by listdate desc

考勤统计未完成处理办法:
select * from flysysprocess  where prostatu=0 for update        (表里查询出来的数据,把相应企业的给删除就行了,记得提交事务。)
还不行将表删除:delete from flysysprocess

日志未完成指令删除:
1、delete flycmdinfo where devsn='07180000000018' and cmdresult=2   (将设备序列号为07180000000018的未完成指令删除;cmdresult是是否执行成功的标记字段,0为成功,2为失败;表后加企业编号查出来都是执行完成了的)
2、truncate table flycmdinfo     (删除整张表)

删除补贴名单里的领取记录:
根据企业编号查 flycgsublist,查出补贴批次号,然后再根据批次号去 详细表 flycgsubdetail 查对应的那条记录,删掉就行。    

消费记录排序语句:
select to_char(listdate,'yyyy-mm') listdate ,count(listmoney),sum(listmoney/100) from flycgdetail61 where listtype = '2101' and listdate >= to_date('2014-01-01 00:00:00','yyyy-mm-dd hh24:mi:ss') and listdate <= to_date('2014-12-20 23:59:59','yyyy-mm-dd hh24:mi:ss') group by to_char(listdate,'yyyy-mm') 

按物理卡号查询一段时间计次消费流水记录:
第一种
select * from flycgdetail61 where empcardno=3290341024 and listdate >= to_date('2016-12-5 23:59:59','yyyy-mm-dd hh24:mi:ss') and  listdate <= to_date('2016-12-14 23:59:59','yyyy-mm-dd hh24:mi:ss') and listmoney=0 order by listdate desc

第二种
select * from flycgdetail61 where empcardno=3290341024 and listdate between to_date('2016-12-5 23:59:59','yyyy-mm-dd hh24:mi:ss') and to_date('2016-12-14 23:59:59','yyyy-mm-dd hh24:mi:ss') and listmoney=0 order by listdate desc

考勤记录绑定人员卡号信息:
第一步查询 empid
select empid from flyattrecord156 where companyid is null group by empid 
第二步  --根据 empid查询卡号
select * from flycardinfo where companyid=156 and empid=23510 and cardstatus=2
第三步  --查询需要补卡号的该人员的考勤记录
select * from flyattrecord156 where companyid is null and empid=23510
第四步    --修改卡号和 公司ID 
update flyattrecord156 set empcardno='2359340357',companyid=156 where companyid is null and empid=23510
update flyattrecord156 set empcardno='2359340357',companyid=156 where companyid is null and empid=23510 and attdate>'2016-06-27'

卡户发卡时间查询:
select a.empno,a.empname,a.empid,b.empcardno,b.updatetime,case when b.cardnum=1 then '发卡' else '注销' end as statu 
from flyemployee a inner join flycardchange b  on a.empid=b.empid where a.companyid=166

在平台增加充值记录:
1、用语句
先对消费记录进行排序
select * from flycgdetail96 where empid = 38880 order by listdate desc
再增加充值记录,这种方法更新时间会是当前时间。
call AddCgdetail('2997253859',44933,40009,1357,3000,3350,1001,'2016-10-27 15:25:36',0,0,0,0,96,'ORCL',1)

这里面参数依次是:卡号,内部卡编号,员工内部编号,部门编号,消费/充值金额,消费/充值后卡内余额,交易类型,交易时间,0,0,0,0,公司编号,更新人员,钱包类型。
中间四个 0 直接填就行。

2、查出消费记录后按格式进行手动添加,参数按充值记录的格式添加(其中cdid需要在上下记录之间,消费额为充值金额,类型为1001,timeid、listid、deven、subid填0,一直到updatetime,后续参数除WLTYPE填1外都不填写)。
select * from flycgdetail96 where empid = 38880 order by listdate desc for update


select * from flyemployee where empname='方俊彪'
select * from flycardinfo where empid='25706'
select * from flycgdetail61 where empcardno='2486079648'and empid=21982 order by listdate desc
update flycgdetail61 set empid=25706 where empid=21982 and empcardno='3295318944'

select * from flyattrecord61 where empcardno='2486079648'
update flyattrecord61 set empid=25706 where empid=21982 and empcardno='3295318944'


查询出只有卡户但没人事信息的语句:
-- 以公司id为6为例  
select * from flycardinfo  where companyid=6 and cardstatus=2 and empid not in 
( select empid from flyemployee where companyid=6)

数据库中备份只有卡户但没人事信息的数据语句
create table flycardinfobak0503 as ( select * from flycardinfo  where companyid=6 and cardstatus=2 and empid not in 
( select empid from flyemployee where companyid=6)
  )

然后再查询下这个表 flycardinfobak0503 看下数据是不是刚查询出来的这些。

考勤记录丢失处理语句:
查出人员
select empid  from flyattrecord6 where attdate>'2016-07-31' and  attdate<'2016-10-01' 
and devsn='6252144900027'  group by empid order by empid asc


查出记录
select * from flyattrecord6 where attdate>'2016-07-31' and  attdate<'2016-10-01' 
and devsn='6252144900027' and empid=243  order by attdate asc, empid asc,atttime asc
 for update

--未领取补贴的人员信息查询
select e.empname as 姓名,f.submoney/100 as 补贴金额,f.updatetime as 补贴时间,s.subbatchname as 补贴名称 from flyemployee e, 
(select * from flycgsubdetail157 f where f.subdatetime is null) f,flycgsublist157 s where e.empid=f.empid and f.subbatchid=s.subbatchid

平台用户总数统计语句:
select count(*) from flyemployee

平台用户信息大数据导出指令
set line 1000         --设置行的长度
 
set pagesize 0        --输出不换页
 
set feedback off      --默认的当一条sql发出的时候,oracle会给一个反馈,比如说创建表的时候,如果成功命令行会返回类似:Table created的反馈,off后不显示反馈
 
set heading off       --不显示表头信息
 
set trimspool on      --如果trimspool设置为on,将移除spool文件中的尾部空
 
set trims on          --去掉空字符
 
set echo off;       --显示start启动的脚本中的每个sql命令,缺省为on
 
set colsep '|'
 
set termout off        --不在屏幕上显示结果
 
spool e:/db1.txt          --记录数据到db1.txt
 
select EMPID,EMPNAME,EMPNO,DEPTID,EMPSEX,EMPPHONE,EMPADRESS,EMPIDCARD,EMPBIRTHDAY,EMPENTERDAY,EMPLEFTDAY,EMPPOST,EMPMARRIAGE,EMPEMAIL,EMPSTATUS,PHOTONAME,SCHEDUID,COMPANYID,SENDFLAG,ISATTMANAGER,TMPSENDFLAG,UPDATEUSER,UPDATETIME,ACRID,SYNCFLAG,MEMBERUSERID,EMPPWD,MODULEINFO,EMPTYPE,CRMSTATU,HAVCHANGEPWD,FULLCODE,POSTID,ACTIVETIME,ADDTIME,APPSTATUS,EMPGUID,CPRID,TSMSEQID,TSMSTATUS,TSMDESC,ALIAS from flyemployee;  --导出数据语句
 
spool off              --收集完毕
 
exit
 


查询最后一次消费或者充值后的余额语句(可替换时间和公司编号,有empID、工号、姓名、卡户余额四列,其中2000-01-01为格式)
select e.empid,e.empno as 工号,e.empname as 姓名,d.cardmoney as 卡户余额 from (select * from (
 select empid,sum(cardmoney/100) as cardmoney from (
select distinct a.cardmoney,b.* from  (select max(a.listdate) as listdate,
a.empid,a.wltype from flycgdetail157 a left join (select max(listdate) as listdate,empid 
from (select max(listdate) as listdate,empid from flycgdetail157 
where listtype=2103 and updatetime<to_date('2017-09-30 23:59:59','yyyy-mm-dd hh24:mi:ss')
 group by empid union select max(listdate) as listdate,empid from flycgdetail157 
 
where listtype=2101 and updatetime<to_date('2017-09-30 23:59:59','yyyy-mm-dd hh24:mi:ss')
 group by empid union select max(listdate) as listdate,empid from flycgdetail157
 
 where listtype=1101 and updatetime<to_date('2017-09-30 23:59:59','yyyy-mm-dd hh24:mi:ss') group by empid) group by empid) b 
 on a.empid=b.empid where a.listdate>=nvl(b.listdate,to_date('2000-01-01','yyyy-mm-dd')) 
 and a.updatetime<to_date('2017-09-30 23:59:59','yyyy-mm-dd hh24:mi:ss') group by a.empid,a.wltype) b 
 inner join  flycgdetail157 a on a.listdate=b.listdate and a.empid=b.empid and a.wltype=b.wltype )
  group by  empid  order by empid ) where cardmoney>=0)d,flyemployee e where e.empid=d.empid

猜你喜欢

转载自blog.csdn.net/qq_40731742/article/details/86596657