is there a way to get SUM of COUNT(*) AS user_id of two tables in one query?
this code shows only table rtv ID : NAME but I need ID: NAME + COUNT SUMMARY
$sql = "SELECT blah.* FROM rtv AS blah LEFT JOIN (SELECT mes_id_fk, COUNT(*) AS user_id FROM vote_ip WHERE date> (NOW() - INTERVAL 2592000 SECOND) GROUP BY mes_id_fk) AS top_voted ON blah.id = top_voted.mes_id_fk ORDER BY top_voted.user_id DESC limit 5";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$list .= "".$row[id].": ".$row[naslov]." <br>";
}
echo $list;
Kevin’s answer is almost the correct one (DEPENDING on the actual desired result… i think you want the individual sums as well? Could be reading it wrong.), but perhaps explaining your tables a little bit more might help; why do you want the count of two seperate tables? There’s probably a better way to design the DB/query to get what you want.
SELECT SUM(sub1.field1, sub2.field2),sub1.field1,sub2.field2 FROM
(SELECT COUNT(*) AS field1 FROM table1) AS sub1,
(SELECT COUNT(*) AS field2 FROM table2) AS sub2;
SELECT users.username,COUNT(vote.up) AS votes_received
FROM users
LEFT JOIN vote
ON users.user_id = vote.user_id AND vote.up = 'Y'
GROUP BY users.user_id
ORDER BY votes_received DESC;
see, giving us all the information gets you better answers!