I have 2 tables: comments & blog
blog has the following fields: id(Unique key),title, author, body, date, img, imgdes, tags
comments : key(Unique key), postid(related to the id of blog),name, email, date, message
Im trying to display all of my blog post and the number of comments on every post.
So im trying to "count(postid) where postid=id"
I got something to work but its based around having 1 comment which wont work but this is it:
SELECT a.postid,c.author,c.title, c.id,c.body,c.date,c.pic, c.tags, c.imgdesc,
COUNT(*) AS num_comments FROM comments a LEFT JOIN blog c ON c.id = a.postid
GROUP BY c.id order by id DESC"
Again this only work when everything has a comment and i get why but i cant figure out how to implement what I want.
To put it all out there i have:
$sql="***( help 1 of 2) what to set the query to****"
$query = mysql_query($sql) or die(mysql_error());
<?php do{ ?>
<html stuff here>
<?php echo $blog['title']?><br>
<?php echo $blog['*******(help 2 of 2) # of comments display here******']
<?php } while($blog = mysql_fetch_assoc($sql));?>
im sure this is a easy join but i have no clue thanks!
Use this query it may works for you
SELECT a.postid,c.author,c.title, c.id,c.body,c.date,c.pic, c.tags, c.imgdesc, COUNT(a.key) AS num_comments FROM blog c left outer join comments a ON a.postid = c.id GROUP BY c.id order by id DESC
if you want all the blog posts then it should be on the left side of the left join.
I think the issue lies where you write: FROM comments a LEFT JOIN blog c ON c.id = a.postid
GROUP BY c.id order by id DESC"
The comments a and blog c don't seem to be correct references.
Should it be FROM comments.a LEFT JOIN blog.c ON ...?
Are you trying to count the number of comments in each blog? if yes then,
SELECT c.postid,count(key) as num_of_comments
FROM blog b, comments c
WHERE b.id = c.postid
GROUP BY c.postid
Another easy way out:
As you have the postid as a foreign key, you can just get the results from comments table
SELECT postid,count(key) as num_of_comments
FROM comments
GROUP BY postid
Related
Hello I am trying to sort through these 3 Tables
I need to create a query that goes through the 'Author' Table,
grabs the author num
Then goes to the 'Wrote' table to find the 'BookCode' from the AuthorNum of the last table
Then to finally go through the Book table to list the title of the book and the first name and last name of the author.
I was thinking of using a join table but am not too solid on my uinderstanding on how it works. Nested select statements was my next guess but I can't get them to go through so many tables.
If anyone could help me that would be fantastic, thank you.
You want to use INNER JOINS to match up the data
SELECT *
FROM authors AS a
INNER JOIN wrote AS w
ON a.AuthorNum = w.AuthorNum
INNER JOIN book AS b
ON w.BookCode = b.BookCode
Please try to use this :
(I named the first table name to first)
Select a.Title as title, w.AuthorFirst as firstName, w.AuthorLast as lastName
From wrote as w
Inner Join author as a
Inner Join first as f
On (Select ww.AuthorNum From WroteTable as ww Order By DESC LIMIT 1) = f.AuthorNum
On f.BookCode = a.BookCode
I have 2 models "post_model" and "comment_model"
in Post_controller I get a result array from post_model which has all the posts.
I'm trying to add comments count for each post but I couldn't and need help.
comment table has post_id.
Please let me know how to handle this.
Thanks in advance.
Try this,
SELECT
p.post_id,
(SELECT count(c.post_id) FROM comments c WHERE c.post_id = p.post_id) cnt
FROM posts p
GROUP BY p.post_id LIMIT 5
Hope this will solve your problem.
Something like this should give you the number of comments for each post:
SELECT
COUNT(*) AS comment_cnt, post_id
FROM comment_table
GROUP BY post_id;
Then you can traverse the result set and enrich the corresponding post objects.
If you want to count comments only for some specific posts and you know their post_id, you could do something like:
SELECT
COUNT(*) AS comment_cnt, post_id
FROM comment_table
WHERE post_id IN (post_id1, post_id2, ...)
GROUP BY post_id;
SELECT p.post_id, p.post_title, p.post_content, p.user_id,count(c.post_id)
FROM posts p
LEFT JOIN comments c USING(post_id)
GROUP BY p.post_id, p.post_title, p.post_content, p.user_id;
I'm trying to get PHP to list out comments by descending number of likes they receive.
Currently, the comments' content and the number of likes they receive are in 2 separate tables: "comments" and "likes".
PHP code:
To get comments from "comments" table:
$this->db->order_by ('comment_id', 'asc');
$data['comment'] = $this->db->select()->get('comment');
To get likes from "likes" table:
$data['like'] = $this->db->get('like');
To show the number of likes for each comment:
$query_like=$this->db->query("select ip from like where comment_id='$comment_id'");
$count_like=$query_like->num_rows();
I'm wondering if it's possible to order the comments by the number of likes they receive without changing the tables' structure. Any advice hugely appreciated.
If I understand the data structure correctly, you just need a join and an aggregation:
select c.*, count(*) as numlikes
from comments c join
upvote l
on c.comment_id = l.comment_id
group by c.comment_id
order by count(*) desc;
EDIT:
To get comments with zero upvotes, do the left outer join in the other direction:
SELECT c.*, count(u.comment_id) as num_upvotes
FROM comment c left join
upvote u
on c.comment_id = u.comment_id
WHERE c.comment_id = '$interview_id'
GROUP BY c.comment_id
ORDER BY num_upvotes DESC;
I am trying to make a comments and replies to comments for media posted by users.
My comments table is structured like:
commentId : parentCommentId : mediaId : userId : comment
I want to select the most recent 10 original comments and their replies to a mediaId.
To do this I am running 2 sql statements.
SELECT commentId FROM comments
WHERE mediaId='3' AND parentCommentId='0'
LIMIT 10;
(This gets the commentIds of the most recent original 10 posts.
I then use these commentIds in the following)...
SELECT c.*,u.* FROM comments AS c
JOIN users AS u on u.userId=c.userId
WHERE parentCommentId IN --( *****commentIds from previous query***** );
Is there a better way to do this? Perhaphs using a JOIN?
What if you make it this way:
SELECT c.*,u.* FROM comments c
JOIN users u on u.userId=c.userId
JOIN comments p_c on p_c.commentId = c.parentCommentId
WHERE p_c.mediaId='3' AND p_c.parentCommentId='0'
LIMIT 10;
This will be useful for your problem
SELECT c.*,u.* FROM comments AS c
INNER JOIN users AS u on u.userId=c.userId
INNER JOIN comments pc on pc.commentId = c.CommentId
WHERE pc.mediaId='3' AND pc.parentCommentId='0'
LIMIT 10;
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',