使用特殊条件更新字段

时间:2019-07-01 02:40:49

标签: sql sql-update

如何在下一种情况下更新用户名(管理员类型)的密码:用户名的前3个字母,姓氏的2个姓氏和手机的4位数字

create table company 
(
    CODE_COMPANY char(30),
    NAME_COMPANY varchar2(30) not null,
    MAIL_COMPANY varchar2(30) null,

    constraint PK_CODE_COMPANY primary key (CODE_COMPANY),
);

create table USERNAME 
(
    NAME_USERNAME varchar2(30),
    USER_LOCATION number,
    fNAME varchar2 (30) not null,
    lNAME varchar2 (30) not null,
    PHONE_USER char(13) null,
    PASSWORD varchar2(30) not null,
    USER_POSITION varchar2 (30),
    check (USER_POSITION in('Admin', 'Superadmin', 'Technician', 'Student')),

    constraint PK_NAME_USERNAME primary key (NAME_USERNAME),
    constraint FK_USER_LOCATION foreign key (USER_LOCATION) references uLOCATION (LOCATION)
);

create table uLOCATION 
(
    LOCATION number,
    CODE_COMPANY char(30),
    NAME_LOCATION varchar2(30) not null,
    FLOOR_LOCATION varchar2(10),
    check (FLOOR_LOCATION in ('MAIN_FLOOR', '1ST FLOOR', '2ND FLOOR', '3RD FLOOR')),

    constraint PK_LOCATION primary key (LOCATION),
    constraint FK_CODE_COMPANY_L foreign key (CODE_COMPANY) references company (CODE_COMPANY),
);

1 个答案:

答案 0 :(得分:1)

Oracle concat函数仅接受两个参数,因此必须有两个嵌套的concat才能将3个字符串连接在一起。

update username set password = 
concat(
  concat(
  substr(fname,1,3),
  substr(lname, length(lname) - 1)
  ),
  substr(phone_user, length(phone_user) - 3)
)
where user_position = 'Admin';

这些链接说明了substr()与length()一起工作的方式

https://www.techonthenet.com/oracle/functions/substr.php

https://www.w3resource.com/oracle/character-functions/oracle-length-function.php