MySQL-在主表中按一定条件计数行,并等于特定值

MySQL-在主表中按一定条件计数行,并等于特定值,mysql,sql,assignment-operator,multiple-tables,Mysql,Sql,Assignment Operator,Multiple Tables,我有两个表,第一个名为表1: ID | Name | Type | isActive | isDeleted | ----------------------------------------------- 1 | item 1 | 4 | 1 | 0 | 2 | item 2 | 2 | 1 | 0 | 3 | item 3 | 1 | 1 | 1

我有两个表,第一个名为表1

ID  |  Name    |  Type | isActive | isDeleted | 
-----------------------------------------------
1   |  item 1  |  4    |  1       |  0        |
2   |  item 2  |  2    |  1       |  0        |
3   |  item 3  |  1    |  1       |  1        |
4   |  item 4  |  1    |  1       |  0        |
5   |  item 5  |  1    |  1       |  0        |
6   |  item 6  |  3    |  1       |  0        |
7   |  item 7  |  1    |  1       |  0        |
8   |  item 8  |  2    |  1       |  0        |
9   |  item 9  |  1    |  1       |  0        |
10  |  item 10 |  1    |  1       |  0        |
ID  |  table1_id  | options    | value
------------------------------------
1   |  1          | dont_ask   |  1   
2   |  2          | dont_ask   |  1
3   |  5          | dont_ask   |  1
4   |  6          | dont_ask   |  1
5   |  8          | alwasys_ask|  1
6   |  9          | alwasys_ask|  1
7   |  1          | is_flagged |  1
8   |  2          | is_flagged |  0
9   |  3          | is_flagged |  0
10  |  4          | is_flagged |  0
11  |  5          | is_flagged |  0
12  |  6          | is_flagged |  1
13  |  7          | is_flagged |  0
14  |  8          | is_flagged |  0
15  |  9          | is_flagged |  0
16  |  10         | is_flagged |  0
第二个名称为表1\u meta

ID  |  Name    |  Type | isActive | isDeleted | 
-----------------------------------------------
1   |  item 1  |  4    |  1       |  0        |
2   |  item 2  |  2    |  1       |  0        |
3   |  item 3  |  1    |  1       |  1        |
4   |  item 4  |  1    |  1       |  0        |
5   |  item 5  |  1    |  1       |  0        |
6   |  item 6  |  3    |  1       |  0        |
7   |  item 7  |  1    |  1       |  0        |
8   |  item 8  |  2    |  1       |  0        |
9   |  item 9  |  1    |  1       |  0        |
10  |  item 10 |  1    |  1       |  0        |
ID  |  table1_id  | options    | value
------------------------------------
1   |  1          | dont_ask   |  1   
2   |  2          | dont_ask   |  1
3   |  5          | dont_ask   |  1
4   |  6          | dont_ask   |  1
5   |  8          | alwasys_ask|  1
6   |  9          | alwasys_ask|  1
7   |  1          | is_flagged |  1
8   |  2          | is_flagged |  0
9   |  3          | is_flagged |  0
10  |  4          | is_flagged |  0
11  |  5          | is_flagged |  0
12  |  6          | is_flagged |  1
13  |  7          | is_flagged |  0
14  |  8          | is_flagged |  0
15  |  9          | is_flagged |  0
16  |  10         | is_flagged |  0
我试图计算表1中满足某些特定条件的行数,其中一些条件

WHERE条件必须包含以下条件:

table1.type = 1 and table1.isActive = 1 and table1.isDeleted = 0 and table1_meta.options = 'is_flagged' and table1_meta.value = 0
这是:

table1_meta.options = 'dont_ask' and table1_meta.value = 1
table1_meta.options = 'always_ask' and table1_meta.value = 1
这是:

table1_meta.options = 'dont_ask' and table1_meta.value = 1
table1_meta.options = 'always_ask' and table1_meta.value = 1
那么,我该怎么做呢

SQLFiddle链接:


谢谢。

我假设您正在尝试计算第一个表中的行数。下面是使用子查询的一种方法:

select count(*)
from table1 t1
where t1.type = 1 and t1.isActive = 1 and t1.IsDeleted = 0 and
      exists (select 1
              from table1_meta tm
              where t1.id = tm.table1_id and tm.options = 'is_flagged' and tm.value = 0
             ) and
      not exists (select 1
                  from table1_meta tm
                  where t1.id = tm.table1_id and
                        tm.options = 'dont_ask' and tm.value = 1
                 ) and
      exists (select 1
              from table1_meta tm
              where t1.id = tm.table1_id and
                    tm.options = 'always_ask' and tm.value = 1
             );

这对元表上的每个条件都有一个单独的子查询。

我想我找到了问题的答案

查询是:

SELECT Count(*) AS total FROM
        (SELECT Count(*)
        FROM   table1 t1,
               table1_meta t1meta
        WHERE  t1.type = 1
               AND t1.isactive = 1
               AND t1.isdeleted = 0
               AND t1meta.options = 'is_flagged'
               AND t1meta.value = 0
               AND t1.id NOT IN (SELECT table1_id
                                 FROM   table1_meta tm
                                 WHERE  tm.options = 'dont_ask'
                                        AND tm.value = 1)
        UNION
        SELECT Count(*)
        FROM   table1_meta tm,
               table1 t1
        WHERE  t1.id = tm.table1_id
               AND tm.options = 'always_ask'
               AND tm.value = 1) x

无论如何,谢谢你,戈登。

是的,第一个表是主表,我正在尝试计算这个表中的行数。谢谢你的回答,但我在查询中看不到“总是问”的条件。我需要计算所有条件下的行数。再次感谢。我想我无法解释我需要什么。对不起我的英语。我需要计算表1中除标记为“Don_ask”之外的所有行,但结果必须(可以更改其他条件)始终包含标记为“always_ask”的行。上面的查询没有提供它。