SQL count() multiple where values

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

我想数数 VOTE1VOTE2 并在我的人口统计中显示出来,如果只是在哪里 VOTE1 它是工作的,但如果我添加 VOTE2 的值不显示。

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

下面是示例代码,当 VOTE2 没有添加。here are the example code when vote2 is not added.

该代码工作时,只有 VOTE1 是在代码中。the code is working when only VOTE1 is in the code

这是我的组合框 this is my combobox

我的 VOTE1VOTE2 我想计数的组合框。选择是YES或NO。我想显示 "是 "或 "否 "的数字。my vote1 and vote2 combobox i want to count. the selection is YES or NO. i want to show the numbers of yes or no.

mysql sql visual-studio-2010 visual-studio-code xampp
1个回答
0
投票

诚然,我认为我只了解这个问题的一部分,所以请原谅我。如果你想分别报告 Vote1 和 Vote2 的结果,那么你需要在选择语句中把它们分开。

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`   ")

如果您试图计算符合您条件的投票1或投票2的住宅,那么我会尝试以下方法

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

0
投票

正如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`   ")

0
投票

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

其中VOTE1 ='" & ComboBox1.SelectedValue & "' OR VOTE2 ='" & ComboBox1.SelectedValue & "'

© www.soinside.com 2019 - 2024. All rights reserved.