Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql SQL-如何联接3个表并选择多个列的计数_Mysql_Sql - Fatal编程技术网

Mysql SQL-如何联接3个表并选择多个列的计数

Mysql SQL-如何联接3个表并选择多个列的计数,mysql,sql,Mysql,Sql,我被困在这种情况下,不知道如何编写select查询以得到我想要的结果。情况是这样的 我有3个表会话、请求、日志 会话表 # session table to store sessions data ID user ------------ 1 John 2 Sarah # request table to store `http` requests come to me ID session ------------ 1 1 2 1 3

我被困在这种情况下,不知道如何编写select查询以得到我想要的结果。情况是这样的

我有3个表会话、请求、日志

会话表

# session table to store sessions data
ID     user
------------
1      John
2      Sarah
# request table to store `http` requests come to me
ID     session
------------
1      1
2      1
3      1
4      1
5      2
6      NULL
# log table to store the logs that I record in my php code and store_
# them in the database while processing the requests 
ID     request
------------
1      1
2      1
3      2
请求表

# session table to store sessions data
ID     user
------------
1      John
2      Sarah
# request table to store `http` requests come to me
ID     session
------------
1      1
2      1
3      1
4      1
5      2
6      NULL
# log table to store the logs that I record in my php code and store_
# them in the database while processing the requests 
ID     request
------------
1      1
2      1
3      2
日志表

# session table to store sessions data
ID     user
------------
1      John
2      Sarah
# request table to store `http` requests come to me
ID     session
------------
1      1
2      1
3      1
4      1
5      2
6      NULL
# log table to store the logs that I record in my php code and store_
# them in the database while processing the requests 
ID     request
------------
1      1
2      1
3      2

我想要的是选择每个会话以及在此会话中发出的请求数,以及日志数。我希望结果是

#sessions result
+----+-------+----------+------+
| ID | USER  | requests | logs |
+----+-------+----------+------+
| 1  | John  |    4     |  3   |
+----+-------+----------+------+
| 2  | Sarah |    1     | NULL |
+----+-------+----------+------+

我所做的是使用连接和按会话分组

select session.*,count(request.id) as requests,count(log.id) as logs
from session left join request on session.id=request.session
left join log on request.id=log.request group by session.id
问题是,当我这样做时,如果一个请求有超过1个日志,那么在我们的情况下,它将被类似于join的请求1复制,该请求有日志1和日志2

# wrong result !! not what I want. notice the count of requests in session 1
+----+-------+----------+------+
| ID | USER  | requests | logs |
+----+-------+----------+------+
| 1  | John  |  ((5))   |  3   |
+----+-------+----------+------+
| 2  | Sarah |    1     | NULL |
+----+-------+----------+------+
我做错了什么


谢谢

请抓住我的方法。我还没有编译这个查询。 我需要强调这一点

(select count(*) from log where request.id=log.request)
完整SQL

select session.id,session.user,count(request.id) as requests,logs from (
 select session.*,(select count(*) from log where request.id=log.request) as     
logs from sesson join
request on sesson.id=request.sesson group by request.id
)group by session.id

试着抓住我的方法。我还没有编译这个查询。 我需要强调这一点

(select count(*) from log where request.id=log.request)
完整SQL

select session.id,session.user,count(request.id) as requests,logs from (
 select session.*,(select count(*) from log where request.id=log.request) as     
logs from sesson join
request on sesson.id=request.sesson group by request.id
)group by session.id

我能说什么?我能说什么呢?谢谢你,先生,我会尝试你的方法谢谢你,先生,我会尝试你的方法