简体   繁体   English

从结果集中的子查询返回记录数

[英]Returning a recordcount from a subquery in a result set

I am attempting to return a rowcount from a subquery as part of a result set. 我正在尝试从子查询返回行数作为结果集的一部分。 Here is a sample that I've tried that didn't work: 这是我尝试过的无效示例:

SELECT recordID SELECT recordID
, GroupIdentifier ,GroupIdentifier
, count( ) AS total ,count( )个,总计
, (SELECT COUNT( ) FROM table WHERE intActingAsBoolean = 1) AS Approved ,(SELECT COUNT( )from table WHERE intActingAsBoolean = 1)已获AS批准

FROM table 从表
WHERE date_format(Datevalue, '%Y%m%d') BETWEEN 'startDate' AND 'endDate' 在'startDate'和'endDate'之间的date_format(Datevalue,'%Y%m%d')

GROUP BY groupIdentifier GROUP BY groupIdentifier

What I'm attempting to return for 'Approved' is the number of records for the grouped value where intActingAsBoolean = 1. I have also tried modifying the where clause by giving the main query a table alias and applying an AND clause to match the groupidentifier in the subquery to the main query. 我试图返回“已批准”的内容是其中intActingAsBoolean = 1的分组值的记录数。我还尝试通过给主查询赋予表别名并应用AND子句来匹配groupidentifier来修改where子句。在主查询的子查询中。 None of these are returning the correct results. 这些都没有返回正确的结果。 The query as written returns all records in the table where intActingAsBoolean = 1. 书面查询将返回表中的所有记录,其中intActingAsBoolean = 1。

This query is being run against a MySQL database. 该查询正在针对MySQL数据库运行。

How about this hack to do it without a subquery: 如何在没有子查询的情况下做到这一点:

SELECT
    recordID,
    GroupIdentifier,
    COUNT() AS total,
    SUM(intActingAsBoolean = 1) AS Approved 
FROM table
WHERE date_format(Datevalue, '%Y%m%d') BETWEEN 'startDate' AND 'endDate'
GROUP BY groupIdentifier

这可能不是最好的方法,但是您可以编写一个函数来返回您要查找的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM