FIND_IN_SET的使用,前面介绍过,distinct是结果去重复的函数,两者结合使用,却花费了我一天的光阴。

1、先面试含有重复值正确的显示顺序
select  lId
from tbCourse1 as a left join tbCourseClassify as b
on a.lId=b.lCourseId
where (a.nUserType=1  or a.nUserType=0) and a.strServiceids like '%.0.%'
and b.nType in (-1,10,9,11,12,18,19,32,1,2,3,4,5,6,7,8,52,53,54,55,56,57,102,103,104,105,106,110,111,109,112,113)
ORDER BY FIND_IN_SET(b.nType,'-1,10,9,11,12,18,19,32,1,2,3,4,5,6,7,8,52,53,54,55,56,57,102,103,104,105,106,110,111,109,112,113')
,b.lOrderIdx desc

2、不含重复值的结果集,顺序已经被distinct(暂且归给distinct)打乱
select distinct(lId)
from tbCourse1 as a left join tbCourseClassify as b
on a.lId=b.lCourseId
where (a.nUserType=1  or a.nUserType=0) and a.strServiceids like '%.0.%'
and b.nType in (-1,10,9,11,12,18,19,32,1,2,3,4,5,6,7,8,52,53,54,55,56,57,102,103,104,105,106,110,111,109,112,113)
ORDER BY FIND_IN_SET(b.nType,'-1,10,9,11,12,18,19,32,1,2,3,4,5,6,7,8,52,53,54,55,56,57,102,103,104,105,106,110,111,109,112,113')
,b.lOrderIdx desc

3、最终正确的结果

select  DISTINCT(lId) from(
select  lId
from tbCourse1 as a left join tbCourseClassify as b
on a.lId=b.lCourseId
where (a.nUserType=1  or a.nUserType=0) and a.strServiceids like '%.0.%'
and b.nType in (-1,10,9,11,12,18,19,32,1,2,3,4,5,6,7,8,52,53,54,55,56,57,102,103,104,105,106,110,111,109,112,113)
ORDER BY FIND_IN_SET(b.nType,'-1,10,9,11,12,18,19,32,1,2,3,4,5,6,7,8,52,53,54,55,56,57,102,103,104,105,106,110,111,109,112,113')
,b.lOrderIdx desc) as c

关于FIND_IN_SET 和distinct 的坑爹的一天的更多相关文章

  1. group_concat函数与find_in_set()函数相结合

    一:group_concat函数详解 1.语法如下: group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'] ...

  2. C# list distinct操作

    使用代理实现对C# list distinct操作   范型在c#编程中经常使用,而经常用list 去存放实体集,因此会设计到对list的各种操作,比较常见的有对list进行排序,查找,比较,去重复. ...

  3. 使用代理实现对C# list distinct操作

    范型在c#编程中经常使用,而经常用list 去存放实体集,因此会设计到对list的各种操作,比较常见的有对list进行排序,查找,比较,去重复.而一般的如果要对list去重复如果使用linq dist ...

  4. Mysql查询用逗号分隔的字段-字符串函数FIND_IN_SET(),以及此函数与in()函数的区别

    查询用逗号分隔的字段,可以用字符串函数FIND_IN_SET(): 查询数据库表中某个字段(值分行显示),可以用函数in(). 今天工作中遇到一个问题,就是用FIND_IN_SET()函数解决的. 第 ...

  5. MySQL巧用FIND_IN_SET和GROUP_CONCAT函数减少Java代码量

    数据库表简介:物品表 `id` int(11)  '物品id,唯一标识', `name` varchar(255) '物品名称', `level` int(11) '物品类别等级,礼品包为最高级1,类 ...

  6. mysql中FIND_IN_SET()和GROUP_CONCAT()的使用

    知识点:mysql中FIND_IN_SET和GROUP_CONCAT()的使用 (一) 场景:当我们使用mysql数据库,查询一张的数据,其中的一列存放的是是另一张表id用“,”拼接的字符串 如下图所 ...

  7. [LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串

    Given a string, find the length of the longest substring T that contains at most k distinct characte ...

  8. [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串

    Given a string S, find the length of the longest substring T that contains at most two distinct char ...

  9. [LeetCode] Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

随机推荐

  1. Shell命令行

    利用wc命令统计文件行,单词数,字符数,利用sort排序和去重,再结合uniq可以进行词频统计. cat file.txt sort hello.c | uniq -c | sort -nr |hea ...

  2. SqlHelper数据库访问类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  3. JS中的Function对象

    Function是函数的原型,所有的函数都来源于Function,获得函数的方法有两种类型,分为动态函数和函数继承. 动态函数 创建一个Function语法: var func = new Funct ...

  4. 3D 转换

    用X.Y.Z分别表示空间的3个维度,三条轴互相垂直.如下图 1.左手坐标系 2.透视(perspective) 电脑显示屏是一个2D平面,图像之所以具有立体感(3D效果),其实只是一种视觉呈现 ,通过 ...

  5. 蜗牛爱课 - iOS7、8模态半透明弹出框

    //源Controller中跳转方法实现 MKDialogController *controller = [[MKDialogController alloc] init]; controller. ...

  6. UVa 202 - Repeating Decimals

    给你两个数,问你他们相除是多少,有无限循环就把循环体括号括起来 模拟除法运算 把每一次的被除数记下,当有被除数相同时第一个循环就在他们之间. 要注意50个数之后要省略号...每一次输出之后多打一个回车 ...

  7. MySQL----information-schema数据库相关权限的说明。

    MySQL中的information_schema数据库比较特别有如下几个要注意的地方. 1.就算是一个新创建的用户,也就是说这个用户只有一个usage权限.它都可以查看informatoin_sch ...

  8. PBOC2.0安全系列之—脱机认证之动态数据认证(DDA)

    动态数据认证: 一,什么是动态数据认证(DDA) 由于上篇<< PBOC2.0安全系列之—脱机认证之静态数据认证(SDA)>>已经对静态数据认证部分做了详细的分析,一些基本知识 ...

  9. 设置windows窗口半透明(使用SetLayeredWindowAttributes API函数)

    所需函数原型:BOOL WINAPI SetLayeredWindowAttributes(HWND hWnd,  COLORREFcrKey,  BYTE bAlpha,  DWORD flag); ...

  10. 基本的Logstash 例子

    基本的Logstash 例子: 为了测试你的Logstash 安装,运行最基本的Logstash 管道: cd logstash-2.3.0 bin/logstash -e 'input { stdi ...