使用 SQL 中的 where 子句计算字段数量
是否可以对某些字段进行计数?
我有以下查询:(
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用子查询轻松完成:
You can do it easily with sub query:
尝试将其添加为一列:
Try adding this as a column: