关于FIND_IN_SET 和distinct 的坑爹的一天
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 的坑爹的一天的更多相关文章
- group_concat函数与find_in_set()函数相结合
一:group_concat函数详解 1.语法如下: group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'] ...
- C# list distinct操作
使用代理实现对C# list distinct操作 范型在c#编程中经常使用,而经常用list 去存放实体集,因此会设计到对list的各种操作,比较常见的有对list进行排序,查找,比较,去重复. ...
- 使用代理实现对C# list distinct操作
范型在c#编程中经常使用,而经常用list 去存放实体集,因此会设计到对list的各种操作,比较常见的有对list进行排序,查找,比较,去重复.而一般的如果要对list去重复如果使用linq dist ...
- Mysql查询用逗号分隔的字段-字符串函数FIND_IN_SET(),以及此函数与in()函数的区别
查询用逗号分隔的字段,可以用字符串函数FIND_IN_SET(): 查询数据库表中某个字段(值分行显示),可以用函数in(). 今天工作中遇到一个问题,就是用FIND_IN_SET()函数解决的. 第 ...
- MySQL巧用FIND_IN_SET和GROUP_CONCAT函数减少Java代码量
数据库表简介:物品表 `id` int(11) '物品id,唯一标识', `name` varchar(255) '物品名称', `level` int(11) '物品类别等级,礼品包为最高级1,类 ...
- mysql中FIND_IN_SET()和GROUP_CONCAT()的使用
知识点:mysql中FIND_IN_SET和GROUP_CONCAT()的使用 (一) 场景:当我们使用mysql数据库,查询一张的数据,其中的一列存放的是是另一张表id用“,”拼接的字符串 如下图所 ...
- [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 ...
- [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 ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- Hadoop书籍下载链接
Hadoop书籍推荐1:Hadoop实战(结合经典案例全面讲解hadoop整个技术体系)http://www.db2china.net/club/thread-25148-1-1.html2:Hado ...
- JavaScript 【非IE DOM2级XML】
DOM2中的XML IE可以实现了对XML字符串或XML文件的读取,其他浏览器也各自实现了对XML处理功能.DOM2级在document.implementaion中引入了createDocument ...
- javascript设计模式——Publish/Subscribe
推荐阅读http://dev.housetrip.com/2014/09/15/decoupling-javascript-apps-using-pub-sub-pattern/ 我们先引出问题的所在 ...
- UVA 10570 Meeting with Aliens
题意: N个外星人围成一桌坐下,有序的排列指N在N-1与N+1中间,现在给出一个序列,问至少交换几次可以得到有序的序列. 分析: 复制一遍输入序列,放在原序列之后.相当于环.通过枚举,可以把最小交换次 ...
- Spring-----事务配置的五种方式
转载自:http://blog.csdn.net/hekewangzi/article/details/51712821
- 使用VIM + Ctags
通常在Linux或其他*Nix环境我们都使用VIM作为代码编辑工具,在纯命令终端下,它几乎是无可替代的. 它具有非常强大的扩展机制,在文字编辑方面基本上无所不能. 不过Emacs用户请不要激动,笔者还 ...
- ie兼容---haslayout
要想更好的理解 css, 尤其是 IE 下对 css 的渲染,haslayout 是一个非常有必要彻底弄清除的概念.大多IE下的显示错误,就是源于 haslayout. 什么是 haslayout ? ...
- HTTP缓存缓存机制
http协议无状态,所以缓存设定从两方面考虑.客户端浏览器和服务器端. 浏览器端实现过期机制. 服务器端实现验证机制. 缓存机制. 为了减轻服务器负担,也减少网络传输数量.http1.0定义了Expi ...
- struts2笔记09-动态方法调用
1.action配置 <action name="testDynamic" class="com.test.actions.TestAction"> ...
- Python爬虫实战(1):爬取Drupal论坛帖子列表
1,引言 在<Python即时网络爬虫项目: 内容提取器的定义>一文我们定义了一个通用的python网络爬虫类,期望通过这个项目节省程序员一半以上的时间.本文将用一个实例讲解怎样使用这个爬 ...