开发者

How do I get this 2 SQL querys to be 1

开发者 https://www.devze.com 2023-01-09 07:09 出处:网络
Tables are: user_table(user_id, user_name) , votes_table(id,user_id, vote_date, votes) $sql = SELECT user_id, sum(votes) AS sumvotes, vote_date

Tables are: user_table(user_id, user_name) , votes_table(id,user_id, vote_date, votes)

$sql = SELECT user_id, sum(votes) AS sumvotes, vote_date 
FROM votes_table 
WHERE vote_date > 1279749600 
ORDER BY sumvotes DESC
LIMIT 10

$result = query($sql)

foreach user_id in $result
    $sql = SELECT user_id, username 
                    FROM user_table u
                    WHERE u.user_id = user_id
$result2 = query($sql)
somearray.add($result AN开发者_如何学运维D $result2)


SELECT user_id, username 
FROM user_table u, (SELECT user_id, sum(votes) AS sumvotes, vote_date 
                    FROM votes_table 
                    WHERE vote_date > 1279749600 
                    ORDER BY sumvotes DESC
                    LIMIT 10) t
WHERE u.user_id = t.user_id


Try:

$sql = SELECT u.user_id, count(v.votes) AS Totalvotes, v.votes, v.vote_date 
FROM v.votes_table inner join user_table u ON v.user_id = u.user_id 
WHERE v.vote_date > 1279749600 
GROUP BY v.votes
ORDER BY Totalvotes DESC
LIMIT 10


SELECT *, SUM(votes) AS sumvotes FROM votes_table LEFT JOIN user_table ON votes_table.user_id = user_table.user_id WHERE vote_date > 1279749600 ORDER BY sumvotes DESC LIMIT 10;
0

精彩评论

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

关注公众号