【问题标题】:Mysql select with multiple conditionMysql选择多个条件
【发布时间】:2019-09-05 07:56:03
【问题描述】:

我需要帮助来创建查询 表A

+------------+--------------------+-------+
| product_id | name               | price |
+------------+--------------------+-------+
|         13 | Product 13         |     5 |
|         14 | Product 14         |     2 |
|         15 | Product 15         |     3 |
|         16 | Product 16         |     2 |
|         17 | Product 17         |    15 |
+------------+--------------------+-------+

表 B

+----+------------+-------------+
| id | product_id | taxonomy_id |
+----+------------+-------------+
| 10 |         13 |           5 |
| 11 |         13 |           2 |
| 12 |         14 |           3 |
| 13 |         15 |           2 |
| 14 |         16 |          15 |
| 14 |         16 |           5 |
| 14 |         16 |          19 |
| 14 |         16 |          21 |
| 14 |         16 |          18 |
+----+------------+-------------+

我的尝试

SELECT *
FROM A
LEFT JOIN B ON B.product_id = A.product_id 
WHERE IF(B.taxonomy_id IN ('5','15'), 
         IF(B.taxonomy_id IN ('2'), 1, 0), 0) = 1
GROUP BY A.product_id

我需要它来将表 A 中正确的结果返回给我

B.taxonomy_id 是“5”或“15”,B.taxonomy_id 是“2”

这个例子的结果是 -> product_id - 13 而且我还需要得到一些结果SELECT count(*) ... -> return is 1

【问题讨论】:

  • 你的预期结果是什么
  • the reason why you should always have a primary/unique key when using innoDB .. 如果没有主键/唯一键,您或多或少会将 InnoDB 的多线程性能降级为锁定的 MyISAM ..
  • 您当前的where 子句只会产生taxonomy_id 不在[5, 15, 2] 中的结果,因为一旦它在B.taxonomy_id IN ('5', '15') 中为真,它就会直接转到B.taxonomy_id IN ('2') 这将产生假,这导致0。我建议将其缩短为B.taxonomy_id IN ('2', '5', 15)

标签: mysql sql join where-clause


【解决方案1】:

而不是在WHERE 子句中进行过滤;您需要在 HAVING 子句中执行此条件过滤。您可以避免使用LEFT JOIN,因为产品应该有它们的分类法(2 AND(5 或 15)):

SELECT a.product_id 
FROM tablea a 
JOIN tableb b on b.product_id = a.product_id 
GROUP BY a.product_id 
HAVING SUM(b.taxonomy_id IN (5,15)) 
       AND SUM(b.taxonomy_id = 2)

结果

| product_id |
| ---------- |
| 13         |

View on DB Fiddle

【讨论】:

  • 问题是当我想得到 -> SELECT COUNT(a.product) ... 返回是 2
  • @user3061527 你为什么要做select count(..)?根据您的原始问题陈述,这不是必需的。请进一步解释,因为您似乎想做一些与原始查询完全不同的事情。
  • 我想要原始查询,也想要获取计数的查询
  • @user3061527 将表格格式的预期结果(就像您提供表格数据的方式一样)添加到问题中。然后只会出现一些清晰度。
  • @user3061527 你不能在同一个查询中获得计数(除非我们使用子查询(Derived Tables 和窗口函数)。但好的是你可以访问 mysqli 对象的num_rows 成员间接获取计数,而您的主查询在单独的行中为您提供所有 product_id。
【解决方案2】:

您的表没有 id 列作为唯一主键是否正常?

无论如何,这是我遇到的,告诉我它是否有效:

SELECT table_nameA.product_id
FROM table_nameA
LEFT JOIN table_nameB on table_nameA.product_id = table_nameB.product_id
WHERE taxonomy_id = 2 AND table_nameA.product_id IN
    (SELECT table_nameA.product_id
    FROM table_nameA
    LEFT JOIN table_nameB on table_nameA.product_id = table_nameB.product_id
    where taxonomy_id = 5 or taxonomy_id = 15
    GROUP BY table_nameA.product_id, taxonomy_id)

结果是:

| product_id |
|------------|
|         13 |

关于你的count查询,完全一样。

SELECT count(table_nameA.product_id) as Quantity
FROM table_nameA
LEFT JOIN table_nameB on table_nameA.product_id = table_nameB.product_id
WHERE taxonomy_id = 2 AND table_nameA.product_id IN
    (SELECT table_nameA.product_id
    FROM table_nameA
    LEFT JOIN table_nameB on table_nameA.product_id = table_nameB.product_id
    where taxonomy_id = 5 or taxonomy_id = 15
    GROUP BY table_nameA.product_id, taxonomy_id)

结果是:

| Quantity |
|----------|
|        1 |

【讨论】:

    【解决方案3】:

    你可以使用exists:

    select a.*
    from a
    where exists (select 1
                  from b
                  where b.product_id = a.product_id and
                        b.taxonomy_id in (5, 15)
                 ) and
           exists (select 1
                  from b
                  where b.product_id = a.product_id and
                        b.taxonomy_id in (2)
                 ) ;
    

    如果你只想要product_ids,那么我建议聚合:

    select b.product_id
    from b
    where b.taxonomy_id in (2, 5, 15)
    group by b.product_id
    having sum( b.taxonomy_id in (5, 15) ) > 0 and
           sum( b.taxonomy_id in (2) ) > 0 ;
    

    【讨论】:

      【解决方案4】:

      您可以使用带有有子句的连接或使用带有 2 个查询的相交函数来获取输出

      13

      在你的情景中。 加入有从句:

      select a.product_id from a inner join b on a.product_id = b.product_id group by a.product_id having (SUM (b.taxonomy_id IN (5,15)) and SUM (b.taxonomy_id in (2)));
      

      与 2 个查询相交:

      select a.product_id from a where (a.product_id IN (select product_id from b where b.taxonomy_id = 2))
      INTERSECT
      select a.product_id from a where (a.product_id IN (select product_id from b where b.taxonomy_id in (5,15)));
      

      对于计数使用类似这样的东西,它将返回

      1

      作为输出:

      select COUNT(*) from (select a.product_id from a where (a.product_id IN (select product_id from b where b.taxonomy_id = 2))
      INTERSECT
      select a.product_id from a where (a.product_id IN (select product_id from b where b.taxonomy_id in (5,15)))) I;
      

      【讨论】:

        猜你喜欢
        • 2014-02-01
        • 2012-03-13
        • 2018-05-06
        • 1970-01-01
        • 2011-03-20
        • 1970-01-01
        • 1970-01-01
        • 2013-04-25
        • 1970-01-01
        相关资源
        最近更新 更多