sql在存储的时候,一个字段中存储最多10次的登录时间信息

14 篇文章 0 订阅
6 篇文章 0 订阅

记录登录系统的时间,最多记录10次,后来添加的时间会覆盖之前的时间,永远只看到最后的10次登录时间。
设计如下:
关键字段:zhxgsj VARCHAR2(500);
数据库表的建设sql

-- Create table
create table tableA
(
  yhdm     VARCHAR2(50) not null,
  tokenval VARCHAR2(250),
  zhxgsj   VARCHAR2(500)
)
tablespace JXYTH
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 7M
    next 1M
    minextents 1
    maxextents unlimited
  );
-- Add comments to the table 
comment on table tableA
  is 'APP_token管理表';
-- Add comments to the columns 
comment on column tableA.yhdm
  is '用户代码';
comment on column tableA.tokenval
  is '登录成功后的token值';
comment on column tableA.zhxgsj
  is '最后登录时间';
-- Create/Recreate primary, unique and foreign key constraints 
alter table tableA
  add constraint PK_tableA primary key (YHDM)
  using index 
  tablespace JXYTH
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 5M
    next 1M
    minextents 1
    maxextents unlimited
  );

java后台存储 zhxgsj 字段值的时候,这么存储:
xml中的sql:

<update id="userlogin.updateTokenByYhdm" parameterClass="java.util.HashMap">
	merge into tableA p using (select count(*) count from tableA where yhdm = #yhdm#) numm on (numm.count > 0)
         when not matched then
			insert into tableA (yhdm, tokenval, zhxgsj) 
				values (#yhdm#, #token#, (select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual) )
         when matched then
			update tableA set zhxgsj = (
			   case when length(zhxgsj) >= 219 then
			           (select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual)
			           ||'&'
			           ||(select substr(zhxgsj,0,199) from tableA where yhdm=#yhdm#)
			        else (
			           (select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual)
			           ||'&'
			           ||(select zhxgsj from tableA where yhdm=#yhdm#)
			        )
			   end 
			)
			where yhdm = #yhdm#
</update>

注意:里面之所以有像219、199这些数字,是因为,一直都是对日期值的格式化严格把控,加上 & 符号,全部在内,计算得来。
219是10个【yyyy-mm-dd hh24:mi:ss】格式的数据和10个【&】符号组成的字符串的长度。

查询最后的登录时间

select substr(zhxgsj,0,19) as zhxgsj from tableA where yhdm = #yhdm#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值