开发者

mysql count from same table and data from the other table

开发者 https://www.devze.com 2023-02-03 23:49 出处:网络
I want to display the name of the registered users with the count of regid by supplying replyid, I don\'t know what will be the correct query to get the results

I want to display the name of the registered users with the count of regid by supplying replyid, I don't know what will be the correct query to get the results

Here are the tables.

details_table

id  regid  replyid  
-------------------
1   1      2  
2   1      3  
6   2      4  
5   3      4  
8   2      5  
9   3      5  
10  4      5  
11  5      5  
12  2      6  
13  6      6  
14  4      6  
15  7      7  
16  8      7  
17  9      7  
18  10     8  
19  2      9  
20  2      10  
21  11     10  
22  12     10  

reg_table

id  regname  
---------------
1      Sam  
2      Ash  
3      Tina  
4      Rohny  
5      Martin  
6      Natasha
7      Natalia
8      Kim
9      Alex
10     John  
11     Neil  
12     Peter  

So if reply开发者_StackOverflowid i.e. (10) is select from details_table by where clause, it's suppose to display the 2,11,12 i.e. (Ash,Neil,Peter) from reg_table with the count of Ash=5,Neil=1,Peter=1


SELECT a.id, a.regname, COUNT(1)
  FROM reg_table a, details_table b,
    details_table c
 WHERE b.replyid=10
   AND b.regid = a.id
     AND c.regid = a.id
GROUP   BY a.id, a.regname


SELECT r.regid, r.regname, count(*)
FROM (
  SELECT DISTINCT regid
  FROM details_table
  WHERE replyid = 10
 ) rg 
 JOIN reg_table r ON rg.regid = r.regid
 JOIN details_table d ON r.regid = d.regid
GROUP BY r.regid


try this

select count(dt.regid) as cnt, regname from details_table dt, reg_table rt where dt.regid = rt.Regid and dt.replyid = 10 group by rt.Regid


SELECT reg_table.regname, count(*) from reg_table,  details_table where details_table.regid = reg_table.id GROUP BY reg_table.id
0

精彩评论

暂无评论...
验证码 换一张
取 消