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.

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

报告相同问题?

悬赏问题

  • ¥15 微信小程序 用oss下载 aliyun-oss-sdk-6.18.0.min client报错
  • ¥15 ArcGIS批量裁剪
  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥15 抖音看过的视频,缓存在哪个文件