【问题标题】:How I can combine two query that will count rows?如何组合两个将计算行数的查询?
【发布时间】:2021-10-18 13:42:43
【问题描述】:

如何将这两个查询合并为一个,谢谢。

$mayorcountqry = $conn->prepare("SELECT VOTE_STATUS FROM voters where VOTE_STATUS = ? AND prcnt_no = ? ");
$mayorcountqry->execute([$MAYOR,$prcnt_no]);
$countmayor = $mayorcountqry->rowCount();   
                                        
$triocountqry = $conn->prepare("SELECT VOTE_STATUS FROM voters where VOTE_STATUS = ? AND prcnt_no = ? ");
$triocountqry->execute([$TRIO,$prcnt_no]);
$triocount= $triocountqry->rowCount();

【问题讨论】:

  • 您可以使用IN 运算符。语法:WHERE [columnName] IN (?, ?).
  • 如何调用每个计数?
  • 你使用聚合函数COUNT(*)
  • 我试过这个 SELECT VOTE_STATUS FROM voters where VOTE_STATUS IN(SELECT COUNT() FROM voters where VOTE_STATUS =? ) 和 VOTE_STATUS IN(SELECT COUNT() FROM voters where VOTE_STATUS = ?
  • 我能知道正确的语法吗谢谢

标签: php sql pdo


【解决方案1】:

你试过 SQL 联合吗?

语法:

SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;

然后你可以只计算每个。

【讨论】:

  • 我已经用 sum 解决了(如果 VOTE_STATUS = ? then 1 else 0 end) AS countmayor,
猜你喜欢
  • 2022-08-04
  • 1970-01-01
  • 1970-01-01
  • 2016-01-16
  • 1970-01-01
  • 2015-06-04
  • 2015-10-08
  • 1970-01-01
  • 2012-11-27
相关资源
最近更新 更多