简体   繁体   English

来自两个不同表的MySQL计数和总和

[英]Mysql count and sum from two different tables

i have a problem with some querys in php and mysql: I have 2 different tables with one field in common: 我在php和mysql中有一些查询问题:我有2个不同的表,其中一个字段相同:

table 1 表格1

id | id | hits | 点击 num_g | num_g | cats | 猫| usr_id |active usr_id |活动

1 | 1 | 10 | 10 | 11 | 11 | 1 | 1 | 53 | 53 | 1 1个

2 | 2 | 13 | 13 | 16 | 16 | 3 | 3 | 53 | 53 | 1 1个

1 | 1 | 10 | 10 | 22 | 22 | 1 | 1 | 22 | 22 | 1 1个

1 | 1 | 10 | 10 | 21 | 21 | 3 | 3 | 22 | 22 | 1 1个

1 | 1 | 2 | 2 | 6 | 6 | 2 | 2 | 11 | 11 | 1 1个

1 | 1 | 11 | 11 | 1 | 1 | 1 | 1 | 11 | 11 | 1 1个

table 2 表2

id | id | usr_id | usr_id | points 点数

1 | 1 | 53 | 53 | 300 300


Now i use this statement to sum just the total from the table 1 every id count + 1 too 现在,我使用此语句对每个id计数+ 1也只汇总表1的总数

SELECT usr_id, COUNT( id ) + SUM( num_g + hits ) AS tot_h FROM table1 WHERE usr_id!='0' GROUP BY usr_id ASC LIMIT 0 , 15

and i get the total for each usr_id 我得到每个usr_id的总数

usr_id| usr_id | tot_h | tot_h |

53 | 53 | 50 50

22 | 22 | 63 63

11 | 11 | 20 20

until here all is ok, now i have a second table with extra points (table2) I try this: 直到这里一切都OK,现在我有了另一个带有加分的表(table2),我尝试这样做:

SELECT usr_id, COUNT( id ) + SUM( num_g + hits ) + (SELECT points FROM table2 WHERE usr_id != '0' ) AS tot_h FROM table1 WHERE usr_id != '0' GROUP BY usr_id ASC LIMIT 0 , 15

but it seems to sum the 300 extra points to all users: 但似乎给所有用户加了300分:

usr_id| usr_id | tot_h | tot_h |

53 | 53 | 350 350

22 | 22 | 363 363

11 | 11 | 320 320

Now how i can get the total like the first try but + the secon table in one statement? 现在,我如何才能像第一次尝试一样获得总计,但在一条语句中加上secon表? because now i have just one entry in the second table but i can be more there. 因为现在我在第二张表中只有一个条目,但是我可以在其中更多。 thanks for all the help. 感谢所有的帮助。


hi thomas thanks for your reply, i think is in the right direction, but i'm getting weirds results, like 嗨,托马斯,谢谢您的答复,我认为方向是正确的,但是我得到的结果很奇怪,例如

usr_id | usr_id | tot_h tot_h

22 | 22 | NULL <== i think the null its because that usr_id as no value in the table2 NULL <==我认为它是null,因为usr_id作为table2中的无值

53 | 53 | 1033 1033

Its like the second user is getting all the the values. 就像第二个用户正在获取所有值。 then i try this one: 然后我尝试这个:

      SELECT table1.usr_id, COUNT( table1.id ) + SUM( table1.num_g + table1.hits + table2.points ) AS tot_h
FROM table1
LEFT JOIN table2 ON table2.usr_id = table1.usr_id
WHERE table1.usr_id != '0'
AND table2.usr_id = table1.usr_id
GROUP BY table1.usr_id ASC          

Same result i just get the sum of all values and not by each user, i need something like this result: 同样的结果,我只是得到所有值的总和,而不是每个用户,我需要像这样的结果:

usr_id | usr_id | tot_h tot_h

53 | 53 | 53 <==== plus 300 points on table1 53 <====加table1上的300点

22 | 22 | 56 <==== plus 100 points on table2 56 <====加table2上的100分

/////////the result i need //////////// //////////我需要的结果/////////////

usr_id | usr_id | tot_h tot_h

53 | 53 | 353 <==== plus 300 points on table2 353 <====在table2上加300点

22 | 22 | 156 <==== plus 100 points on table2 156 <====加上table2的100点

I think the structure need to be something like this Pseudo statements ;) 我认为结构需要像这样的伪语句;)

from table1 count all id to get the number of record where the usr_id are then sum hits + num_g and from table2 select the extra points where the usr_id are the same as table1 and get the result: 从table1计数所有id以获取usr_id为总和+ num_g的记录数,并从table2选择usr_id与table1相同的加点,并得到结果:

usr_id | usr_id | tot_h tot_h

53 | 53 | 353 353

22 | 22 | 156 156

There is nothing in your subquery which calculates extra points to correlate it to the outer Table1. 子查询中没有任何东西可以计算额外的点以将其与外部Table1相关联。 So, one solution is to add that correlation: 因此,一种解决方案是添加相关性:

SELECT usr_id
    , COUNT( id ) + SUM( num_g + hits ) 
    + (SELECT points 
        FROM table2 
        WHERE table2.usr_id = table1.usr_id ) AS tot_h 
FROM table1 
WHERE usr_id != '0' 
GROUP BY usr_id ASC 
LIMIT 0 , 15

Another solution would be to simply join to it directly: 另一个解决方案是直接将其直接加入:

SELECT table1.usr_id
    , COUNT( table1.id ) 
        + SUM( table1.num_g + table1.hits + table2.points ) 
        AS tot_h 
FROM table1 
    Left Join table2
        On table2.usr_id = table1.usr_id
WHERE table1.usr_id != '0' 
GROUP BY table1.usr_id ASC 
LIMIT 0 , 15

i think get the solution, i dont know if its the best but it works for me, if you know a way to optimize this i really apreciate it. 我想得到解决方案,我不知道它是否是最好的,但是对我有用,如果您知道一种优化方法,我真的很感激。

SELECT usr_id , COUNT( id ) + SUM( num_g + hits )as sumtot ,
      (SELECT points FROM table2  WHERE usr_id = table1.usr_id ) AS tot_h
FROM table1
WHERE usr_id != '0'
GROUP BY usr_id ASC

with this i get something like this 有了这个我得到这样的东西

usr_id |sumtot | usr_id | sumtot | tot_h tot_h

5 |557 | 5 | 557 | NULL 空值

53 |2217 | 53 | 2217 | 300 300

So then i just sum the result and show it in a while loop. 因此,然后我只是对结果求和并在while循环中显示它。

<?php
//some mysql here
//then the while loop
// and then the final sum

$final_result=$r_rank['tot_h']+$r_rank['sumtot'];

 ?>

Thanks a lot for your help thomas :) 非常感谢您对托马斯的帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM