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

  • 相关阅读:
    ArchLinux新版本(pacstrap安装)及国内较优源推荐
    [转载]打造自己喜欢的Linux桌面----archlinux
    ArchLinux下LXDE的安装与设置心得
    [转载]linux 文件改名,移动
    SaaS(软件即服务)架构设计
    临时表空间
    DTCMS自定义标签,获取所有栏目以及获得二级子栏目导航
    Druid简介
    jeecg 模糊查询
    JEECG中的模糊查询
  • 原文地址:https://www.cnblogs.com/qiyongliang/p/3365201.html
Copyright © 2011-2022 走看看