使用 SQL 中的 where 子句计算字段数量

发布于 2024-11-29 00:51:37 字数 886 浏览 1 评论 0原文

是否可以对某些字段进行计数?

我有以下查询:(

SELECT * FROM 
(SELECT messages.id as id,
       messages.user_id,
       messages.category_id,
       messages.parent_id,
       messages.message,
       messages.create_date,
       messages.update_date,
       messages.status,
       users.name as username,
       users.id as userid,
       users.email as useremail,
       users.phone as userphone,
       users.active as useractive,
       users.role as userroel,
       users.date as register_date
FROM `afo_messages` as messages 
INNER JOIN `key_users` as users ON messages.user_id = users.id
WHERE messages.category_id=5 AND messages.parent_id=0
ORDER BY messages.id DESC LIMIT 30) ilv 
ORDER BY id ASC

这是输出示例: http://lab.mirgorod.us/res.pdf

是否可以在此查询中再添加一列:子消息计数(parent_id = 父消息的 id)?

is it possible to count fields where something?

I have following query:

SELECT * FROM 
(SELECT messages.id as id,
       messages.user_id,
       messages.category_id,
       messages.parent_id,
       messages.message,
       messages.create_date,
       messages.update_date,
       messages.status,
       users.name as username,
       users.id as userid,
       users.email as useremail,
       users.phone as userphone,
       users.active as useractive,
       users.role as userroel,
       users.date as register_date
FROM `afo_messages` as messages 
INNER JOIN `key_users` as users ON messages.user_id = users.id
WHERE messages.category_id=5 AND messages.parent_id=0
ORDER BY messages.id DESC LIMIT 30) ilv 
ORDER BY id ASC

(Here is example of output: http://lab.mirgorod.us/res.pdf)

Is it possible to add one more column to this query: Count of messages that are childs (parent_id = id of parent message)?

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

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

发布评论

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

评论(2

对你的占有欲 2024-12-06 00:51:37

您可以使用子查询轻松完成:

SELECT * FROM 
(SELECT messages.id as id,
       messages.user_id,
       messages.category_id,
       messages.parent_id,
       messages.message,
       messages.create_date,
       messages.update_date,
       messages.status,
       users.name as username,
       users.id as userid,
       users.email as useremail,
       users.phone as userphone,
       users.active as useractive,
       users.role as userroel,
       users.date as register_date,
       (SELECT COUNT(*) FROM afo_messages WHERE parent_id=messages.id) AS `sub messages`
FROM `afo_messages` as messages 
INNER JOIN `key_users` as users ON messages.user_id = users.id
WHERE messages.category_id=5 AND messages.parent_id=0
ORDER BY messages.id DESC LIMIT 30) ilv 
ORDER BY id ASC

You can do it easily with sub query:

SELECT * FROM 
(SELECT messages.id as id,
       messages.user_id,
       messages.category_id,
       messages.parent_id,
       messages.message,
       messages.create_date,
       messages.update_date,
       messages.status,
       users.name as username,
       users.id as userid,
       users.email as useremail,
       users.phone as userphone,
       users.active as useractive,
       users.role as userroel,
       users.date as register_date,
       (SELECT COUNT(*) FROM afo_messages WHERE parent_id=messages.id) AS `sub messages`
FROM `afo_messages` as messages 
INNER JOIN `key_users` as users ON messages.user_id = users.id
WHERE messages.category_id=5 AND messages.parent_id=0
ORDER BY messages.id DESC LIMIT 30) ilv 
ORDER BY id ASC
猫瑾少女 2024-12-06 00:51:37

尝试将其添加为一列:

child_count = (select count(*) from afo_messages where parent_id=messages.id)

Try adding this as a column:

child_count = (select count(*) from afo_messages where parent_id=messages.id)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文