Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQLite:不同表上的两个不同计数_Sqlite_Count - Fatal编程技术网

SQLite:不同表上的两个不同计数

SQLite:不同表上的两个不同计数,sqlite,count,Sqlite,Count,我很确定你们有一个简单快速的解决方案来解决我的问题,但我的SQL技能有限,我自己也无法解决 所以我得到的是: 新闻组:[姓名(主键)] 文章:[Id(PK)][新闻组名称(FK)][日期:日期][阅读:布尔] 我想知道的是一个查询,它会给我每个新闻组的名称以及未读文章的数量、所有文章的数量和最近一篇文章的日期。。。 这甚至可以在单个Select查询中归档吗 提前谢谢你们 我猜是这样的: SELECT name, COUNT(countUnread) AS countUnread, COUNT(c

我很确定你们有一个简单快速的解决方案来解决我的问题,但我的SQL技能有限,我自己也无法解决

所以我得到的是:

新闻组:[姓名(主键)]

文章:[Id(PK)][新闻组名称(FK)][日期:日期][阅读:布尔]

我想知道的是一个查询,它会给我每个新闻组的名称以及未读文章的数量、所有文章的数量和最近一篇文章的日期。。。 这甚至可以在单个Select查询中归档吗


提前谢谢你们

我猜是这样的:

SELECT name, COUNT(countUnread) AS countUnread, COUNT(countRead) AS countRead, MAX(mostRecentDateUnread) AS mostRecentDateUnread, MAX(mostRecentDateRead) AS mostRecentDateRead FROM (
    SELECT newsgroupName AS name, COUNT(newsgroupName) AS countUnread, 0 AS countRead, MAX(date) AS mostRecentDateUnread, NULL AS mostRecentDateRead
    FROM Article
    WHERE read = 0
    GROUP BY newsgroupName, read

    UNION ALL

    SELECT newsgroupName AS name, 0 AS countUnread, COUNT(newsgroupName) AS countRead, NULL AS mostRecentDateUnread, MAX(date) AS mostRecentDateRead
    FROM Article
    WHERE read = 1
    GROUP BY newsgroupName, read
)
GROUP BY name

我还没有尝试过,但理论上,如果进行一些修复,它可能会起作用。

我猜是这样的:

SELECT name, COUNT(countUnread) AS countUnread, COUNT(countRead) AS countRead, MAX(mostRecentDateUnread) AS mostRecentDateUnread, MAX(mostRecentDateRead) AS mostRecentDateRead FROM (
    SELECT newsgroupName AS name, COUNT(newsgroupName) AS countUnread, 0 AS countRead, MAX(date) AS mostRecentDateUnread, NULL AS mostRecentDateRead
    FROM Article
    WHERE read = 0
    GROUP BY newsgroupName, read

    UNION ALL

    SELECT newsgroupName AS name, 0 AS countUnread, COUNT(newsgroupName) AS countRead, NULL AS mostRecentDateUnread, MAX(date) AS mostRecentDateRead
    FROM Article
    WHERE read = 1
    GROUP BY newsgroupName, read
)
GROUP BY name

我没有尝试过,但在理论上,通过一些修复,它可以工作。

您只需使用适当的聚合函数:

SELECT newsgroupName,
       SUM(NOT read) AS countUnread,
       COUNT(*)      AS countAll,
       MAX(date)     AS mostRecentDate
FROM Article
GROUP BY newsgroupName;

您只需使用适当的聚合函数:

SELECT newsgroupName,
       SUM(NOT read) AS countUnread,
       COUNT(*)      AS countAll,
       MAX(date)     AS mostRecentDate
FROM Article
GROUP BY newsgroupName;

这包括所有文章和所有未读文章的数量吗?@HugoCodes对不起,我错过了那部分。现在看看,谢谢!我一回到家就会检查这是否有效。我不需要未读/已读文章的日期。。只有最后一个;)但我想我可以自己弄明白。内部选择是否存在任何潜在的性能问题?这是否包括所有文章和所有未读文章的计数?@HugoCodes抱歉,我错过了这一部分。现在看看,谢谢!我一回到家就会检查这是否有效。我不需要未读/已读文章的日期。。只有最后一个;)但我想我可以自己弄明白。内部选择是否存在任何潜在的性能问题?这正是我所要求的!如果我在一个带有room数据库和liveobjects的android应用程序中实现这一点,我唯一需要考虑的是性能的影响。无论如何谢谢你!这正是我想要的!如果我在一个带有room数据库和liveobjects的android应用程序中实现这一点,我唯一需要考虑的是性能的影响。无论如何谢谢你!