在CONCAT_WS之前订购 [英] Order before CONCAT_WS

查看:65
本文介绍了在CONCAT_WS之前订购的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含3个Phone列的表,我需要运行一些魔术来尝试获取一些记录,使它们之间的电话号码可以匹配,问题是电话号码可能位于2条记录之间的不同字段上.

I have a table with 3 Phone columns, I need to run some magic trying to get some records where the phone numbers could match between them, the problem is a Phone number could be on different field between the 2 records.

因此,我认为带有3个电话号码的规范字符串应该可以进行比较,问题是规范化过程.有没有办法做到这一点?我正在添加一个片段来说明我需要做什么.

So I think that a canonical string with the 3 phone numbers should allow me to make the comparison, the problem is the canonizing process. Is there a way to do this? I'm adding a snippet to illustrate what I need to do.

表是:

╔═════╦══════════╦══════════╦══════════╗
║ ID  ║  Phone1  ║  Phone2  ║  Phone3  ║
╠═════╬══════════╬══════════╬══════════╣
║ 123 ║ 555-1234 ║ 666-1235 ║          ║
║ 124 ║ 666-1235 ║          ║ 555-1234 ║
║ 125 ║ 555-8520 ║ 777-7410 ║ 444-9999 ║
╚═════╩══════════╩══════════╩══════════╝

我想要的结果是

╔═════╦══════════════════════════════╗
║ ID  ║         ConcatPhones         ║
╠═════╬══════════════════════════════╣
║ 123 ║ 555-1234||666-1235           ║
║ 124 ║ 555-1234||666-1235           ║
║ 125 ║ 444-9999||555-8520||777-7410 ║
╚═════╩══════════════════════════════╝

无论如何,我可以使用CONCAT_WS的简单变体或高效的存储过程来做到这一点吗?

Is there anyway I can do this with a simple variation of CONCAT_WS or a highly efficient stored procedure?

推荐答案

如果值确实为NULL且不为空,则应执行以下操作:

The following should work, if the values are indeed NULL and not blank:

select id,
       concat_ws('||', Phone1, Phone2, Phone3)
from t

参考文献是此处 :

CONCAT_WS()不会跳过空字符串.但是,它会跳过任何 分隔符参数之后为NULL值.

CONCAT_WS() does not skip empty strings. However, it does skip any NULL values after the separator argument.

要处理订购,我会去:

select id,
       group_concat(phone Separator '||' order by phone) as ConcatPhones
from (select t.id,
             (case when nums.n = 1 then phone1
                   when nums.n = 2 then phone2
                   when nums.n = 3 then phone3
              end) as phone
       from t cross join
            (select 1 as n union all select 2 union all select 3) nums
     ) p
where phone is not null
group by id

这将过滤掉没有电话号码的所有ID.

This will filter out any id that doesn't have a phone number.

您也可以使用巨大的case语句来执行此操作,尽管这似乎是一场噩梦:

You can also do this with a giant case statement, although that seems something of a nightmare:

select t.id,
       (case when phone1 < phone2 and phone2 < phone3 then concat_ws('||', phone1, phone2, phone3)
             when phone1 < phone3 and phone3 < phone2 then concat_ws('||', phone1, phone3, phone2)
              . . .  through the remaining 4 permuatiations when all three are present
             when phone1 is null and phone2 < phone3 then concat_ws('||', phone2, phone3)
             . . . through the remaining 5 permutuations when one is NULL
             when phone1 is null and phone2 is null then phone3
              . . . through the remaining 2 permutations when two are NULL
        end) as ConcatPhones
from t

这更有效. 3个电话号码是可行的.我不想处理其中的五个.

This is more efficient. It is feasible for 3 phone numbers. I wouldn't want to deal with, say, five of them.

这篇关于在CONCAT_WS之前订购的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆