dopnpoh056622 2016-06-11 03:51
浏览 102
已采纳

在sql查询中满足条件时,使用前三个字符的***显示字段的结果

I have a table that look like this :

Username   |   Status
aaa        |   Pending
bbbbbbb    |   Pending
cccc       |   Cancelled
dddddddd   |   Cancelled
eeeeee     |   Approved
ffffff     |   Approved

the result that I'd like to show at the end is like this :

Status    | Username
Pending   | ***aaa, ***bbbb
Cancelled | ***cccc, ***ddddd 
Approved  | ***eee, ***fff

I've tried to do this select query

SELECT distinct status,CASE WHEN LENGTH(username) >=6 THEN GROUP_CONCAT(replace(username, left( username, 3 ) , '***') SEPARATOR ', ') ELSE GROUP_CONCAT('***',username SEPARATOR ', ') END AS Username FROM table group by status

However, the result of bbbbbbb from my query won't work because there is 3 characters username at first row. so, the result become like this :

Status    | Username
Pending   | ***aaa, ***bbbbbbb       // wrong (it should be ***aaa , ***bbbb)
Cancelled | ***cccc, ***ddddd // true
Approved  | ***eee, ***fff   // true

How do I perform the query to show the result that I expected?. thank you before

  • 写回答

1条回答 默认 最新

  • down2323 2016-06-11 06:14
    关注

    As I understand, you need to move CASE inside GROUP_CONCAT. Something like this:

    SELECT DISTINCT status,
      GROUP_CONCAT(
        CASE
          WHEN LENGTH(username) >= 6 THEN REPLACE(username, LEFT(username, 3), '***')
          ELSE CONCAT('***', username)
        END SEPARATOR ', '
      )AS Username
    FROM table
    GROUP BY status
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据