I'm trying to get count of posts
in "posts" table,
i have id,topic_id (topic_id= the topic id)
in "topics" table,
i have id,f_id (f_id= the forum id )
i try while and while, but not wor
how can i count the posts?
for ex:
<php?
$test2=mysql_query("SELECT *, COUNT(topic_id) AS post_count FROM posts t LEFT JOIN topics p ON p.id = t.id WHERE `f_id`='1' GROUP BY t.id") or die (mysql_error());
?>
Try this query :
SELECT *, COUNT(topic_id) AS post_count
FROM posts t
LEFT JOIN topics p
ON p.id = t.id AND `f_id`='1'
GROUP BY t.id
Related
I am working on an sql query where I have three tables posts, users and comments. I want to display all posts with its users and number of comments on this. I have following query but it is giving me wrong result:
SELECT
c.userid, count(c.userid), p.postid
FROM comments c, posts p
where c.userid = p.userid group by c.userid
In addition to above query I also require firstname and lastname from users table.
Try something like this,
SELECT
u.userid, u.firstname, u.lastname,p.post, p.postid,
count(c.userid) totalcoments -- may be c.commentid
FROM users u
JOIN posts p ON p.userid=u.userid
LEFT JOIN comments c OM c.postid=p.postid
GROUP BY u.userid, u.firstname, u.lastname,p.post, p.postid
Try something like the following:
SELECT
postid
, p.userid
, COALESCE((
SELECT COUNT( * ) FROM comments WHERE postid = p.id
), 0 ) AS cnt_postid
, COALESCE( ( SELECT CONCAT( firstname, ' ', lastname ) FROM users WHERE userid = p.userid ), 'N/A' ) AS NAME
FROM posts p
LEFT JOIN comments c ON c.postid = p.id
GROUP BY postid
ORDER BY postid
you are probably getting the same amount of count because you not using a group by.
GROUP BY must always be used when using aggregate function. What the group by does is that it will select all unique posts and the the count will return the number of users for that one unique post
This is my table structure in database
I want to get number of likes for each post by counting post field in likes table and display it for every post using foreach loop.
My question is
Is there a way i can do this with one query with JOIN, GROUP BY and COUNT tables and not create multiple queries.
select p.id, p.title, p.content, count(l.id) as likes_count
from posts p
left join likes l on l.post = p.id
group by p.id, p.title, p.content
http://sqlfiddle.com/#!9/bca8ee/1
SELECT p.*, COUNT(l.id)
FROM posts p
LEFT JOIN likes l
ON l.post = p.id
GROUP BY p.id
I have two tables :
posts : id,title,content,show,created_at
comments: id,post_id,created_at
I'm trying to order posts by most commented.
SELECT *, COUNT(comments.id) AS total_comments
FROM comments LEFT JOIN posts ON posts.id = comments.post_id
WHERE posts.show = '1'
GROUP BY complains.id
ORDER BY total_comments DESC
The problem is that the posts with 0 comments don't appear.
Any help would be much appreciated.
With your join above, you are incorrectly joining to get commens that have posts
You should have done a right join or swap the tables in left join like below.
Select *, COUNT(comments.id) as total_comments
FROM posts
LEFT outer JOIN comments on posts.id = comments.post_id
WHERE posts.show = '1'
GROUP BY posts.id
ORDER BY total_comments DESC
You need to do a RIGHT JOIN instead of a LEFT JOIN. Or swap the tables in the LEFT JOIN clause.
While there are many ways to solve this, I think this code is easy to read and understand.
The query in the LEFT JOIN can be copied out and run on its own to help debug. Then you join that result set to the posts table and order the results.
SELECT p.*, IFNULL(c.total_comments, 0) as total_comments
FROM posts p
LEFT JOIN (select post_id, count(post_id) as total_comments from comments group by post_id) as c ON p.id = c.post_id
WHERE p.show = '1'
ORDER BY c.total_comments DESC
I got this code:
SELECT
topics.id,
topics.id_first,
posts.id_first
FROM topics
LEFT
JOIN posts
ON posts.id = topics.id_first_msg
My intention, is to do something like this:
SELECT
topics.id,
topics.id_first,
posts.id_first,
posts.id_last
FROM topics
LEFT
JOIN posts
ON posts.id = topics.id_first_msg
LEFT
JOIN posts
ON posts.id = topics.id_last_msg
But, when I try to do Left Join twice, I get an error. Which is the correct way then? Thanks.
You need to provide aliases for the table you're joining more than once:
SELECT
topics.id,
topics.id_first,
p1.id_first,
p2.id_last
FROM topics
LEFT JOIN posts p1 ON p1.id = topics.id_first_msg
LEFT JOIN posts p2 ON p2.id = topics.id_last_msg
You have to alias the second left join;
SELECT
topics.id,
topics.id_first,
posts.id_first,
posts2.id_last
FROM topics
LEFT
JOIN posts
ON posts.id = topics.id_first_msg
LEFT
JOIN posts AS posts2
ON posts2.id = topics.id_last_msg
The posts table is ambiguous.
First off - do you really need to join twice? Can't you just do this:
SELECT
topics.id,
topics.id_first,
posts.id_first,
posts.id_last,
posts.id_first,
posts.id_last
FROM topics
LEFT
JOIN posts
ON posts.id = topics.id_first_msg
Second - if you really have to join twice - give the posts table two different aliases (you'll still need to edit the selected columns)
On the home page of my website I want to display the latest posts to the forum however I don't want to show the same topic twice. How can I modify the code below to do this?
http://punbb.informer.com/wiki/punbb13/integration#recent_10_posts
Basically show the latest posts, but only once for each forum topic/thread.
Add a condition to keep only records where the post is the last post in the topic:
WHERE p.id = (
SELECT pp.id
FROM posts AS pp
WHERE pp.topic_id = t.id ORDER BY pp.posted DESC LIMIT 1
)
If you want only one value per topic, you could group by topic, and from each topic select the most recent post. Then, you could choose the top 10 topics.
I'll write it in SQL, and you can translate that to PHP:
SELECT p.id, p.message, o.subject
FROM
((SELECT t.id
FROM posts AS p LEFT JOIN topics AS t ON p.topic_id = t.id
GROUP BY t.id
HAVING p.posted = MAX(p.posted) ) ids LEFT JOIN topics AS t ON ids.id = t.id) o
LEFT JOIN posts AS p ON o.id = posts.topic_id
ORDER BY p.posted DESC
LIMIT '0,10'
change this line
'SELECT' => 'p.id, p.message, t.subject',
to
'SELECT DISTINCT' => 'p.id, p.message, t.subject',