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

  • 相关阅读:
    使用PHP类库PHPqrCode生成二维码
    40+个对初学者非常有用的PHP技巧
    (高级篇)jQuery学习之jQuery Ajax用法详解
    lerna管理前端模块实践
    Node.js:深入浅出 http 与 stream
    从koa-session源码解读session本质
    Elastichsearch实践
    Linux代理搭建TinyProxy
    linux常用命令
    node前后端同构的踩坑经历
  • 原文地址:https://www.cnblogs.com/qiyongliang/p/3365201.html
Copyright © 2011-2022 走看看