English 中文(简体)
合并三个表格的 Mysql 查询
原标题:Mysql query for joining three tables
  • 时间:2012-05-24 16:20:15
  •  标签:
  • mysql

我有三张桌子

请求 - 请求_ id, 请求_ 消息, 请求者_ id

  • requesters
  • requesters_id, requesters_name
  • responses
  • response_id, request_id, response_message

我想从请求表 < / enger > 中获取所有请求 < / enger > 、 请求者 < strong > 请求者 < / strong > 请求者姓名 < / enger > 中请求者身份所在的字段,并从回复表中获取对请求 < id < / strong > 答复的 < nuger > 数。

帮帮我吧

谢谢 谢谢

最佳回答
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
问题回答

我从下选人那里得到点数 然后加入另外两张桌子 加上经常的组合

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 




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签