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

  • 相关阅读:
    pandas
    使用Selenium模拟浏览器抓取淘宝商品美食信息
    Pycharm安装第三方库
    shift 函数
    mysql 基础(一)
    mysql基础(三)
    Python Programming for Finance
    进程线程
    股票上市代码及上市时间
    SQLyog连接数据库报错plugin caching_sha2_password could not be loaded
  • 原文地址:https://www.cnblogs.com/qiyongliang/p/3365201.html
Copyright © 2011-2022 走看看