我可以计算连接表中的行数吗? [英] Can I count number of rows in joined table?
本文介绍了我可以计算连接表中的行数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
像这样:
SELECT s.*, count( logs.* ) as ssh_count
FROM servers s
LEFT JOIN logs ON s.ip_address = logs.server_ip
但我在该查询中遇到错误:
But I get an error with that query:
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在 '* 附近使用的正确语法作为 ssh_count FROM servers s LEFT JOIN logs ON s.ip_address = logs.server_ip LIMIT' 在第 1 行
我认为这是因为您无法在 count 函数中寻址表.
I think that's because you can't address a table in the count function.
我可以使用子查询执行此操作,但这可能会降低查询速度.这样做的更好方法是什么?
I can do this using a subquery, but that will likely slowly down the query. What is a better way of doing this?
推荐答案
你可以寻址一个表列,但你不能寻址table.*
,你可以这样做,例如:>
You can adress a table column, but you can't address table.*
, you can do this for example:
SELECT s.*, count( logs.server_ip ) as ssh_count
FROM servers s
LEFT JOIN logs ON s.ip_address = logs.server_ip
这篇关于我可以计算连接表中的行数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文