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 无源定位系统的时差估计误差标准差
  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
  • ¥15 通过GaussianView进行结构微调消除虚频
  • ¥15 调用transformers库
  • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
  • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
  • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
  • ¥15 excel 上下按钮 显示行
  • ¥20 云卓h12pro 数传问题