求和两个复杂的选择

时间:2017-05-19 12:16:14

标签: php mysql sum

我正在寻找这两个选择的总和,但我不知道该怎么做。你能帮帮我吗?



$score_selection = "SELECT user_ID, Sum(approval_rate) AS total_approval FROM comments GROUP BY user_ID ORDER BY total_approval DESC";
	$score_result = mysqli_query ($connect,$score_selection) ;

	while ($comment_score = mysqli_fetch_array($score_result)) { 
		$user_ID = $comment_score['user_ID'] ;
		$comment_score = $comment_score['total_approval'] ;
    
    $score_checked_query = "SELECT user_score FROM user_score WHERE user_ID='$user_ID'" ;
			$score_checked_result = mysqli_query ($connect,$score_checked_query) ;

			while ($score = mysqli_fetch_array($score_checked_result)) {
			$user_score = $score['user_score'] ;



			echo "($user_score+$comment_score) point(s) </br>" ;
      
      <?php
	}
	}
	?>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

简单使用加入无需多次查询

SELECT c.user_ID, sum(c.approval_rate+u.user_score) AS total_approval 
FROM comments AS c 
JOIN user_score AS u 
ON u.user_ID=c.user_ID 
GROUP BY c.user_ID 
ORDER BY total_approval DESC