关于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 ...
随机推荐
- C#委托 Lamda表达式
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 超级强大的SVG SMIL animation动画详解
本文花费精力惊人,具有先驱前瞻性,转载规则以及申明见文末,当心予以追究.本文地址:http://www.zhangxinxu.com/wordpress/?p=4333 //zxx: 本文的SVG在有 ...
- js触摸屏案例
js 手机端触发事事件.javascript手机端/移动端触发事件 处理Touch事件能让你跟踪用户的每一根手指的位置.你可以绑定以下四种Touch事件: 1 2 3 4 touchstart: ...
- Gabor滤波器学习
本文的目的是用C实现生成Gabor模版,并对图像卷积.并简单提一下,Gabor滤波器在纹理特征提取上的应用. 一.什么是Gabor函数(以下内容含部分翻译自维基百科) 在图像处理中,Gabor函数是一 ...
- Python基础:11.2_函数调用
我们已经接触过函数(function)的参数(arguments)传递.当时我们根据位置,传递对应的参数.这种参数传递的方式被称为函数参数的位置传递. 我们将接触更多的参数传递方式. 回忆一下位置传递 ...
- console.log几个小知识
<script> //百度的console console.log('一张网页,要经历怎样的过程,才能抵达用户面前?\n一位新人,要经历怎样的成长,才能站在技术之巅?\n探寻这里的秘密:\ ...
- Android 不通过USB数据线调试的方法
在开发Android应用时,通常情况下是通过USB数据线连接设备和计算机,但对于一些需要使用USB设备的应用,这种方法就碰到了麻烦,手机的USB接口已经和外接的USB设备连接,无法再连数据线,此时可以 ...
- digital root问题
问题阐述会是这样的: Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- Linux 下搭建ftp服务器 指定用户指定目录及其他操作
搭建 Linux下 rpm -qa |grep vsftpd查看是否安装 没安装yum安装 /etc/vsftpd/目录下有vsftpd.conf配置文件 根据需求 进行配置 是否使用匿名用户以及文 ...
- Android显示YUV图像
需要流畅显示YUV图像需要使用Opengl库调用GPU资源,网上在这部分的资料很少.实际上Android已经为我们提供了相关的Opengl方法 主体过程如下: 1.建立GLSurfaceView 2. ...