0

我有三张桌子。

请求- request_id、request_message、requester_id

  • 请求者
  • requesters_id, requesters_name
  • 回应
  • response_id、request_id、response_message

我想从 requests 中获取所有字段requesters_name 从 requester_id在 requests 表中的 requesters 中获取,并从响应表中获取 request_id 的响应数。

请帮帮我。

谢谢

4

2 回答 2

0

我会从子选择中获取计数,然后将其他两个表与常规连接一起连接

SELECT r.*, rn.requesters_name, 
    (SELECT COUNT(response_id) 
       FROM responses rp WHERE r.request_id=rp.request_id) 
    AS response_count
FROM requests r JOIN requester rn ON r.requesters_id=rn.requesters_id 
于 2012-05-24T16:27:27.487 回答
0
SELECT requests.*,requesters.requesters_name,count(1) AS c FROM requests
LEFT JOIN requesters ON requesters.requester_id = request.requester_id
LEFT JOIN responses ON responses.request_id = requests.request_id
GROUP BY requests.request_id
于 2012-05-24T16:30:27.527 回答