且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL:从基于 2 个字段的选择中删除重复值

更新时间:2023-02-02 21:18:35

对于这个特定问题 max(language) 应该有效,因为 d 小于 e.

For this particular question max(language) should work because d is less than e.

编辑(不会破坏的解决方案):

EDIT (Sollution that won't break):

添加max(case language when 'default' then 0 else 1 end)

Edit2(更好):

添加 max(case language when 'english' then 1 else 0 end) 这样,如果向 in()中添加更多语言,您甚至不会得到重复值> 部分

Add max(case language when 'english' then 1 else 0 end) so you won't even get diplicate values if you add more languages to your in() section

编辑编号.2:

正如我所承诺的,我现在有了一个全新的星期一早晨,并得到了满足您需求的正确查询:-)

As i promised, here i am with a fresh new mondaymorning mind and got the right query for your needs :-)

试试

SELECT value,
       CASE group_concat(language)
         WHEN 'default' then 'default'
         ELSE LTRIM(REPLACE(group_concat(language SEPARATOR ' '),'default',''))
       END as language,
       externID
FROM demo
WHERE externID = 10
AND   language in ('default','english')
GROUP BY value;

说明:GROUP_CONCATLTRIM