doubingqi5829 2018-02-05 11:52
浏览 263
已采纳

Mysql查询IN子句返回结果

Good Day! All Fridays,

I have some problem in my sql query. I'm using IN class with subquery like this

SELECT
  cm.category_id,
  cd.name
FROM
  category_master cm,
  category_detail cd,
  brand_to_categories b2c
WHERE
  cm.category_id = b2c.category_id
  AND
  cd.category_id = cm.category_id
  AND
  cd.language_id = 1
  AND
  cm.status <> 2
  AND
  cm.category_id IN (SELECT DISTINCT sub_dd.categories FROM distribution_master bdm, distribution_detail bdd, subscription_category_to_brand_user sub_dd WHERE bdd.distribution_id = bdm.distribution_id AND bdm.distributor_id = 35 AND bdd.brand_id = 7191 AND sub_dd.sub_d_id = bdd.id)
  AND
  b2c.brand_id = 7191;

The following is the sub-query which is creating problem for me.

cm.category_id IN (
SELECT DISTINCT 
sub_dd.categories 
FROM 
distribution_master bdm, 
distribution_detail bdd, 
subscription_category_to_brand_user sub_dd 
WHERE 
bdd.distribution_id = bdm.distribution_id 
AND 
bdm.distributor_id = 35 
AND 
bdd.brand_id = 7191 
AND 
sub_dd.sub_d_id = bdd.id)

the result of the sub-query is like this.

3913,4517,6059,7137,7138,7139,7140,7141,7144

this result is coming from only single row in the target table because I stored these ids as string in the filed.

Now the problem is this, I can not get results of the all categories. Main query final result only return one category information which category_id is 3913. But if I run this query manually with sub-query values instead of the sub-query then it returns all the categories results.

Manual query with sub-query values is like this

SELECT
  cm.category_id,
  cd.name
FROM
  category_master cm,
  category_detail cd,
  brand_to_categories b2c
WHERE
  cm.category_id = b2c.category_id
  AND
  cd.category_id = cm.category_id
  AND
  cd.language_id = 1
  AND
  cm.status <> 2
  AND
  cm.category_id IN (3913,4517,6059,7137,7138,7139,7140,7141,7144)
  AND
  b2c.brand_id = 7191;

Please help me regarding this problem.

Sorry I forget, I'm using Mysql

  • 写回答

1条回答 默认 最新

  • doutiaoku4495 2018-02-05 11:54
    关注

    Assuming you are using MySQL, use FIND_IN_SET:

    WHERE
        ...
        FIND_IN_SET(cm.category_id,
                    (SELECT DISTINCT sub_dd.categories
                     FROM distribution_master bdm,
                          distribution_detail bdd,
                          subscription_category_to_brand_user sub_dd
                     WHERE bdd.distribution_id = bdm.distribution_id AND
                           bdm.distributor_id = 35 AND
                           bdd.brand_id = 7191 AND
                           sub_dd.sub_d_id = bdd.id)) > 0
    

    If you are using SQL Server, then we have to do a bit more work:

    WHERE ',' + (SELECT DISTINCT ...) + ',' LIKE '%,' + cm.category_id + ',%'
    

    General comment: Avoid storing CSV data in your SQL tables. MySQL almost made the problem worse by offering FIND_IN_SET and making it easier to skirt good table design.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?