选择两列不等于0的计数

问题描述 投票:0回答:1

我有一个如下所述的表:

|----------------|-------------|-------------|
|      a_id      |     m_id    |   e_id      |
|----------------|-------------|-------------|
|       1        |       1     |      0      |
|       2        |       2     |      2      |
|       3        |       3     |      3      |
|       4        |       1     |      1      |
|       5        |       4     |      4      |
|----------------|-------------|-------------|

我需要一个执行a_id应该具有m_ide_id不等于0的任务的查询。

因此,我编写了如下查询,该查询返回计数5:

SELECT count(a_id) from ids where (e_id!=0 or m_id!=0)

我需要a_id应该具有不同的m_id != 0。那应该返回计数4。

mysql sql
1个回答
0
投票

使用count(distinct

select count(distinct m_id) from ids where e_id!=0 or m_id!=0
© www.soinside.com 2019 - 2024. All rights reserved.