像这样:
SELECT s.*, count( logs.* ) as ssh_count
FROM servers s
LEFT JOIN logs ON s.ip_address = logs.server_ip
但是我的查询出错:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* ) as ssh_count FROM servers s LEFT JOIN logs ON s.ip_address = logs.server_ip LIMIT' at line 1
我认为那是因为你无法在count函数中寻址表。
我可以使用子查询来执行此操作,但这可能会慢慢查询。 有什么更好的方法呢?
答案 0 :(得分:3)
您可以找到表格列,但无法解决table.*
,例如可以执行此操作:
SELECT s.*, count( logs.server_ip ) as ssh_count
FROM servers s
LEFT JOIN logs ON s.ip_address = logs.server_ip