SQL count() 多个 where 值

SQL count() multiple where values

我想计算 VOTE1VOTE2 并显示在我的人口统计中。如果它只在 VOTE1 的地方工作,但如果我在 DataGrid 上添加 VOTE2 值不显示:

jokenfindthis("SELECT 
                  ` PRK` as 'Purok'
                    ,COUNT(*)  as 'No. of households'  
                FROM `tblresidence` 
                where VOTE1 & VOTE2   ='" & ComboBox1.SelectedValue & "'  
                GROUP BY `Purok`   ")

下面是未添加VOTE2时的示例代码:

代码中只有 VOTE1 时代码有效:

这是我的组合框:

我的VOTE1VOTE2ComboBox我要算了。选择是或否。我想显示是或否的数字:

正如 pagalprogrammer 提到的,您可能希望将 WHERE 语句分成两部分:

jokenfindthis("SELECT 
                  ` PRK` as 'Purok'
                    ,COUNT(*)  as 'No. of households'  
                FROM `tblresidence` 
                where VOTE1 ='" & ComboBox1.SelectedValue & "'  
                      AND VOTE2 ='" & ComboBox1.SelectedValue & "'  
                GROUP BY `Purok`   ")

通过选择 VOTE1 = AND VOTE2 =,您只会看到 VOTE1 也等于 VOTE2 的结果。你真的想这样做吗?

在哪里 投票 1 ='" & ComboBox1.SelectedValue & "'
或 VOTE2 ='" & ComboBox1.SelectedValue & "'

诚然,我想我只是部分理解了这个问题,所以请提前原谅。如果您尝试分别报告 Vote1 和 Vote2 的结果,则需要在 select 语句中将它们分开。

jokenfindthis("SELECT 
                  ` PRK` as 'Purok'
                    , sum(case when VOTE1 ='" & ComboBox1.SelectedValue & "' then 1 else 0 end)  as 'No. of households - Vote 1'  
                    , sum(case when VOTE2 ='" & ComboBox2.SelectedValue & "' then 1 else 0 end)  as 'No. of households - Vote 2' 
                FROM `tblresidence` 
                where VOTE1 ='" & ComboBox1.SelectedValue & "'  
                      or VOTE2 ='" & ComboBox2.SelectedValue & "'  
                GROUP BY `Purok`   ")

如果您要计算 Vote1 或 Vote2 满足您条件的住宅,那么我会尝试以下方法

jokenfindthis("SELECT 
                  ` PRK` as 'Purok'
                    , count(*)  as 'No. of households'  
                FROM `tblresidence` 
                where VOTE1 ='" & ComboBox1.SelectedValue & "'  
                      or VOTE2 ='" & ComboBox1.SelectedValue & "'  
                GROUP BY `Purok`   ")