来自两个不同表的Mysql计数和总和
我对php和mysql中的一些查询有问题: 我有两个不同的表,有一个共同的字段: 表1 id |点击|数量| g |猫| usr | U id |激活 1 | 10 | 11 | 1 | 53 | 1 2 | 13 | 16 | 3 | 53 | 1 1 | 10 | 22 | 1 | 22 | 1 1 | 10 | 21 | 3 | 22 | 1 1 | 2 | 6 | 2 | 11 | 1 1 | 11 | 1 | 1 | 11 | 1 表2 id | usr | U id |点 1 | 53 | 300来自两个不同表的Mysql计数和总和,mysql,count,Mysql,Count,我对php和mysql中的一些查询有问题: 我有两个不同的表,有一个共同的字段: 表1 id |点击|数量| g |猫| usr | U id |激活 1 | 10 | 11 | 1 | 53 | 1 2 | 13 | 16 | 3 | 53 | 1 1 | 10 | 22 | 1 | 22 | 1 1 | 10 | 21 | 3 | 22 | 1 1 | 2 | 6 | 2 | 11 | 1 1 | 11 | 1 | 1 | 11 | 1 表2 id | usr | U id |点 1 | 5
现在我使用这个语句来计算表1中每个id计数+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
我得到每个usr_id的总数
usr|u id|tot|h|
53 | 50
22 | 63
11 | 20
在这里一切正常之前,现在我有了第二张表和额外的积分(表2)
我试试这个:
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
但它似乎给所有用户加了300分:
usr|u id|tot|h|
53 | 350
22 | 363
11 | 320
现在,我怎样才能在一个语句中得到像第一次尝试的总和+secon表?因为现在我在第二个表中只有一个条目,但我可以在那里有更多条目。
谢谢你的帮助
嗨,托马斯,谢谢你的回复,我认为这是正确的方向,但我得到了奇怪的结果,比如 usr|u id|tot|h
22 | NULL子查询中没有计算额外点以将其与外部表1关联的内容。因此,一种解决方案是增加这种相关性:
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
另一个解决方案是直接加入:
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
子查询中没有计算额外点以将其与外部表1关联的内容。因此,一种解决方案是增加这种相关性:
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
另一个解决方案是直接加入:
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
我想得到解决方案,我不知道它是否是最好的,但它对我有效,如果你知道一种优化方法,我真的很欣赏它
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
有了这个,我得到了这样的东西
usr|u id|sumtot|tot|h
5 | 557 |空
53 | 2217 | 300
然后我将结果相加,并在while循环中显示
<?php
//some mysql here
//then the while loop
// and then the final sum
$final_result=$r_rank['tot_h']+$r_rank['sumtot'];
?>
非常感谢您的帮助thomas:)我想您能找到解决方案,我不知道这是否是最好的,但它对我来说很有效,如果您知道一种优化方法,我真的很欣赏它
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
有了这个,我得到了这样的东西
usr|u id|sumtot|tot|h
5 | 557 |空
53 | 2217 | 300
然后我将结果相加,并在while循环中显示
<?php
//some mysql here
//then the while loop
// and then the final sum
$final_result=$r_rank['tot_h']+$r_rank['sumtot'];
?>
非常感谢你的帮助托马斯:)