Can you use Count(*) in an inner join? - mysql

Currently I have the following database:
Table 1: Customer_Stores
unique_id
page_address
date_added
guide_summary
user_name
cover_photo
guide_title
Table 2: Customer_Stories_Likes
story_id
likex
The 'like' column in the second table contains a 1 or a 0 to indict whether or not a user has liked a post.
What I'd like to do is join these two tables together with 'post_id' and count all of the 'likes' for all the posts based on post_id and order these by how many likes each post got. Is this possible with a single statement? or is it better to use a Count(*) to first determine how many likes each post has?

Yes, it's possible, but you don't need an inner join, because you don't actually need the posts table to do it.
SELECT post_id, count(like) AS post_likes
FROM likes
WHERE like = 1
GROUP BY post_id
ORDER BY post_likes DESC
If you need other information from the posts table as well, you could join it to a subquery that gets the like counts.
SELECT posts.*, like_count
FROM
posts LEFT JOIN
(SELECT post_id, count(like) AS like_count
FROM likes
WHERE like = 1
GROUP BY post_id) AS post_likes
ON posts.post_id = post_likes.post_id
ORDER BY like_count DESC
I used LEFT JOIN rather than INNER JOIN, you can use INNER JOIN if you don't want to include posts with no likes.

Related

I want to get all posts which has more than 250 likes(likes are stored in different tabe)

I have two mysql database table's posts and post_likes...I want to get all posts which has more than 250 likes
right now i am using this query :-
SELECT posts.*, #total_likes := COUNT(nft_likes.id) as total_likes FROM posts inner join nft_likes on nft_likes.nft_id=posts.auction_id where #total_likes>1 group by posts.id
This is the first time i have asked a question.so pls forgive for bad way of telling
post_likes table schema
post table schema
In the WHERE clause you can only refer to a row's data. The result of a COUNT, however, refers to the aggregation of several rows. Use the HAVING clause for limitations on these results.
SELECT
p.*,
COUNT(l.id) AS total_likes
FROM posts p
INNER JOIN nft_likes l ON l.nft_id = p.auction_id
GROUP BY p.id
HAVING COUNT(l.id) > 1
ORDER BY p.id;

MySQL Join And Select Random Field

I want to join two tables and return a random field from the second table because there are multiple matches when joining.
For example. I have a users table and a user_posts table. I want to select each user's id, and a random post id and the post's message that they have in the user_posts table. Each user can have multiple posts in the user_posts table.
This answer explains what I'm trying to do, but it's not working. Here's my query:
SELECT user_id, post_id, message FROM (
SELECT users.id AS user_id, user_posts.id AS post_id, message
FROM users INNER JOIN user_posts ON users.id = user_id
ORDER BY RAND()
) AS a GROUP BY user_id
For testing, I added two rows in the user_posts table for user with id of 1 but it's retrieving the same post every time.
using cross apply to get random one post and join to users to display
select u.id,RandomPostbyUser.*
from users u
cross apply (
SELECT user_posts.id AS post_id, message
FROM users u1 INNER JOIN user_posts ON u1.id = u.id
order by RAND()
LIMIT 1) RandomPostbyUser

How can these two queries be combined?

Sorry for the poor question title, I'm not sure of the correct terminology to describe what I'm asking.
I have two tables:
posts:
p_id | p_author | p_text
post_comments:
pc_id | pc_p_id | pc_author | pc_text
The pc_p_id for each comment corresponds to the p_id of the post.
I want to select:
The p_id and p_text for all of the posts from a specific author
The number of comments for the corresponding post
I can do the first part with a query like this (supposing "1" is an author):
SELECT p_id, p_text FROM posts WHERE p_author = 1
And I can get the number of comments for a specific post like this (supposing "12" is a post id):
SELECT COUNT(*) FROM post_comments WHERE pc_p_id = 12
My question is, how can I combine the two queries so that I get the p_id and p_text for all of the posts from a specific author, along with the number of comments for the corresponding posts?
I tried using a LEFT JOIN like the following, but it gives me a syntax error:
SELECT t1.p_id, t1.p_text, t2.COUNT(*)
FROM posts t1 LEFT JOIN post_comments t2
WHERE t1.p_author = 1 AND t2.pc_p_id = t1.p_id
ORDER BY t1.p_id DESC
Here is the SQL Fiddle:
http://sqlfiddle.com/#!2/e9b975
SELECT p_id, p_text, count(1)
FROM posts p
JOIN post_comments pc
ON p.p_id = pc.pc_p_id
WHERE p_author = 1
GROUP BY p_id, p_text
Here is the edited version that does an embedded select:
SELECT p_id, p_text,
(SELECT COUNT(1) FROM post_comments WHERE pc_p_id = p.p_id) AS count
FROM posts p
WHERE p_author = 1
I verified this in your fiddle
LEFT_JOIN is used when you want all rows from the left-hand table, but there may not be corresponding rows in the right-hand table. Otherwise you want a straight (inner) join. Also, when using an aggregation function like COUNT() you have to use a GROUP_BY clause if you have any non-aggregate columns selected.
SELECT p.p_id, p.p_text, count( pc.pc_id ) AS CountFROM posts p, post_comments pcWHERE pc.pc_p_id = p.p_idAND p.p_author =1AND pc.pc_p_id =12

getting the maxium count of comments in terms of users using mysql?

i have this 4 tables:
posts{id,post,date}
comment{id, user_id,post_id, comment, date}
tag_post{tag_id,post_id}
users{user_id, email,pwd,username}
i want to make this complex query, i want to get the maxiumum number of commenters(users) from a certain topic:
i.e.
select the most commeneters(count) on posts that have been tagged with tag_id=39
LIMIT 5
thanks :))
What about something like this :
select users.user_id, count(*) as nb_comments
from posts
inner join tag_posts on tag_posts.post_id = posts.id
inner join comment on comment.post_id = posts.id
inner join users on users.user_id = comment.user_id
where tag_posts.tag_id = 39
group by users.user_id
order by count(*) desc
limit 5
It should get you the five users who commented the most on posts that have the tag 39.

How to left join multiple one to many tables in mysql?

i have a problem with joining three tables in mysql.
lets say we have a table named posts which I keep my entries in it, i have a table named likes which i store user_id's and post_id's in and a third table named comments which i store user_id's and post_id's and comment's text in it.
I need a query that fetches list of my entries, with number of likes and comments for each entry.
Im using this query:
SELECT posts.id, count(comments.id) as total_comments, count(likes.id) as total_likes
FROM `posts`
LEFT OUTER JOIN comments ON comments.post_id = posts.id
LEFT OUTER JOIN likes ON likes.post_id = posts.id
GROUP BY posts.id
but there is a problem with this query, if comments are empty for an item, likes count is just ok, but lets say if an entry has 2 comments and 4 likes, both total_comments and total_likes will be "8", meaning that mysql multiplies them.
I'm confused and I dont know what whould I do.
Thanks in advace.
Use count(distinct comments.id) and count(distinct likes.id), provided these ids are unique.
Well this is one way to approach it (assuming mysql allows derived tables):
SELECT posts.id, comments.total_comments, likes.total_likes
FROM `posts`
LEFT OUTER JOIN (select post_id, count(id) as total_comments from comments) comments
ON comments.post_id = posts.id
LEFT OUTER JOIN (select post_id, count(id) as total_likes from likes) likes
ON likes.post_id = posts.id
You could also use correlated subqueries. You may want a case statment inthere to account for putting in a 0 when there are no matched records.
Let's try a correlated subquery:
SELECT posts.id,
(select count(Id) from comments where post_id = posts.id) as total_comments,
(select count(Id) from likes where post_id = posts.id) as total_likes
FROM `posts`