Can someone help me, please? I want to count all the occurrences of "username" field from 2 tables(comments_table & comment_replies) that are not more than a week old. After I get the result, I now want to get the "profile_image" of each username from the "users" table.
comments_table looks like this :
Below is the comment_replies table
comment_replies looks like this:
I have already done 80% of the job by writing this query:
SELECT username, COUNT(*) AS total
FROM (
SELECT username FROM comments_table WHERE date_time > NOW() - INTERVAL 1 WEEK
UNION ALL
SELECT username FROM comment_replies WHERE date_time > NOW() - INTERVAL 1 WEEK
) table1
GROUP BY username ORDER BY total DESC
And it runs perfectly well by giving this output:
Here is my problem: I want to join this result to the "users" table so that I can get the "profile_image" for each username. The "users" table has the username column as a unique column:
I know i can use the username column to join but I'm just stuck. Please help. I have searched on Stock Overflow and found this and this but they didn't address my situation.
You just need to join user table with your query:
SELECT u.username,u.profile_image,t.total
FROM `users` u
INNER JOIN (
SELECT username, COUNT(*) AS total
FROM (
SELECT username FROM comments_table WHERE date_time > NOW() - INTERVAL 1 WEEK
UNION ALL
SELECT username FROM comment_replies WHERE date_time > NOW() - INTERVAL 1 WEEK
) table1
GROUP BY username) t
ON u.username=t.username
ORDER BY t.total;
I corrected the spelling of keyword INNER.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.