zoukankan      html  css  js  c++  java
  • 关于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

  • 相关阅读:
    软件工程第一次作业
    单调队列
    八皇后问题
    蓝桥-区间K大数查询
    putchar()和getchar()使用解析
    C++中的各种进制转换函数汇总及学习
    第五次团队作业
    第二次团队作业
    确定团队开发项目
    结对编程之设计电梯控制程序
  • 原文地址:https://www.cnblogs.com/qiyongliang/p/3365201.html
Copyright © 2011-2022 走看看