计算子查询中的行数

发布于 2024-10-24 02:09:27 字数 628 浏览 1 评论 0原文

如何将 SELECT 查询中的行计数为值? 如

SELECT FUCNTIONIMLOOKINGFOR(SELECT * FROM anothertable) AS count FROM table;

因此,count 是子查询 SELECT * FROM anothertable 返回的行数的整数。

编辑

SELECT p.PostPID, p.PostUID, p.PostText, p.PostTime, u.UserUID, u.UserName, u.UserImage, u.UserRep,
    (
        SELECT COUNT(f.FlagTime)
            FROM Flags as f 
                JOIN Posts as p 
                ON p.PostPID = f.FlagPID
    ) as PostFlags
    FROM Posts AS p
        JOIN Users AS u
        ON p.PostUID = u.UserUID
    ORDER BY PostTime DESC
    LIMIT 0, 30

How could I count rows from a SELECT query as a value?
Such as

SELECT FUCNTIONIMLOOKINGFOR(SELECT * FROM anothertable) AS count FROM table;

So that count is an integer of how many rows the subquery SELECT * FROM anothertable returns.

EDIT

SELECT p.PostPID, p.PostUID, p.PostText, p.PostTime, u.UserUID, u.UserName, u.UserImage, u.UserRep,
    (
        SELECT COUNT(f.FlagTime)
            FROM Flags as f 
                JOIN Posts as p 
                ON p.PostPID = f.FlagPID
    ) as PostFlags
    FROM Posts AS p
        JOIN Users AS u
        ON p.PostUID = u.UserUID
    ORDER BY PostTime DESC
    LIMIT 0, 30

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只想待在家 2024-10-31 02:09:27
SELECT ( SELECT COUNT(id) FROM aTable ) as count FROM table

我假设您的示例是实际查询的截断版本,因此也许您应该发布您想要的内容以获得可能更优化的查询。

编辑

直接从我的大脑工作,这样的事情应该是更优化的。

SELECT p.PostPID, p.PostUID, p.PostText, p.PostTime, u.UserUID, u.UserName, u.UserImage, u.UserRep, COUNT(v.FlagTime) as postFlags
    FROM Flags as f 
    JOIN Posts as p ON p.PostPID = f.FlagPID
    JOIN Users AS u ON p.PostUID = u.UserUID
LIMIT 0, 30
GROUP BY p.PostPID
ORDER BY PostTime DESC
SELECT ( SELECT COUNT(id) FROM aTable ) as count FROM table

I assume your example is a truncated version of your actual query, so perhaps you should post what you are after to get a, possibly, more optimal query.

EDIT

Working directly from my brain, something like this should be more optimal.

SELECT p.PostPID, p.PostUID, p.PostText, p.PostTime, u.UserUID, u.UserName, u.UserImage, u.UserRep, COUNT(v.FlagTime) as postFlags
    FROM Flags as f 
    JOIN Posts as p ON p.PostPID = f.FlagPID
    JOIN Users AS u ON p.PostUID = u.UserUID
LIMIT 0, 30
GROUP BY p.PostPID
ORDER BY PostTime DESC
郁金香雨 2024-10-31 02:09:27

您可以指定

SELECT COUNT(*) FROM anothertable

哪个将返回一个数值,您可以在另一个查询中使用该数值,例如在另一个查询的选择列表中,或者作为另一个查询中的条件。

SELECT someVariable FROM table
WHERE (SELECT COUNT(*) FROM anotherTable) > 5

或者

SELECT someVariable, (SELECT COUNT(*) FROM anotherTable) as count FROM table

You can say

SELECT COUNT(*) FROM anothertable

which will return a numeric value, which you can use in another query, such as in the select list of another query, or as a condition in another query.

SELECT someVariable FROM table
WHERE (SELECT COUNT(*) FROM anotherTable) > 5

OR

SELECT someVariable, (SELECT COUNT(*) FROM anotherTable) as count FROM table
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文