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. Mysql安装时出现APPLY security settings错误

    在安装mysql数据库时,如果重新安装,很容易遇见apply security setting error(access denied for user 'root@localhost'(using  ...

  2. Linux学习1——首次登录

    一.写在前面 在本节将介绍首次登录Linux系统(本文中为CentOS)所需要了解的一些基本操作.二.完成目标 1.了解GNOME和KDE窗口管理程序 2.使用在线求助man和info 3.基本命令操 ...

  3. PHP zip压缩文件及解压

    PHP zip压缩文件及解压 利用ZipArchive 类实现 只有有函数.界面大家自己写 ZipArchive(PHP 5.3 + 已自带不需要安装dll) /** * 文件解压 * @param ...

  4. Android_使用getIdentifier()获取资源Id

    Android 获取资源ID的另外一种方法,常规获取ID是在特定的文件夹下面的资源,如果在比较特殊的文件夹下面,就需要其他方法获取ID 了: 使用getIdentifier()方法可以方便的获各应用包 ...

  5. CmdParse

    Procedure URPOSE Uses Dos,Crt; Const VersionNum = 'V1.0 BETA'; ProgNameStr = 'NEWPROJ.EXE'; ProgName ...

  6. Python读取PDF内容

    1,引言 晚上翻看<Python网络数据采集>这本书,看到读取PDF内容的代码,想起来前几天集搜客刚刚发布了一个抓取网页pdf内容的抓取规则,这个规则能够把pdf内容当成html来做网页抓 ...

  7. Python 自学笔记(二)第一个程序 Hello World

    一 打印 Hello world 1,输入 Python “Hello world” 即可 2,脚本文件输出Hello World 在命令行(cmd),输入 python 文件路径+文件名 3,为什么 ...

  8. 转载:10 Easy Steps to a Complete Understanding of SQL

    10 Easy Steps to a Complete Understanding of SQL 原文地址:http://tech.pro/tutorial/1555/10-easy-steps-to ...

  9. [Django 1.5] Django 开发学习资源链接

    jQuery : jQuery API introduction:http://api.jquery.com/ jQuery plugins: http://benalman.com/projects ...

  10. C语言单片和C#语言服务器端DES及3DES加密的实现

    原文:C语言单片和C#语言服务器端DES及3DES加密的实现 公司最近在做单片机和C#语言的通信.用的是Socket通信.传输的数据是明文,后来 在会上讨论准备用DES加密(对称加密)来做. 双方约定 ...