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.

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

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来