我有兩個表,並首次命名表1:MySQL的 - 計算與某些條件在主表中的行和等於特定值
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 |
和第二名爲table1_meta:
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
所以,我該怎麼辦呢?
SQLFiddle鏈接:http://sqlfiddle.com/#!2/2eb27b
感謝。
是的,第一個表是主表,我試圖計算這個表中的行。感謝您的回答,但我在查詢中看不到'** always_ask **'條件。我需要對所有條件的行進行計數。再次感謝。 – 2014-09-21 18:29:33
我猜不出我需要什麼。對不起我的英語不好。我需要計算table1中除了標記爲'** dont_ask **'以外的所有行,但結果必須(可以更改其他條件)一直包含標記爲「always_ask」的標記。上面的查詢不提供它。 – 2014-09-21 19:05:37