I am having trouble with this query and am hoping someone can help.
SELECT
myTable.id,
myTable.subject,
myTable.upvotes,
myTable.downvotes,
(SELECT COUNT(id)
FROM myTable
WHERE myTable.thread = myTable.id) AS comments_count
FROM myTable
Basically I have a table with posts and comments, the comments thread is tied to the id of the original post. In my query I want to show how many relpies (how many threads = id) from all rows for the current id/row.
I hope that makes sense :)
You need to specify the the table with new alias to match in your subquery other wise it will match the thread with id from same table
SELECT
m.id,
m.subject,
m.upvotes,
m.downvotes,
(SELECT COUNT(id)
FROM myTable
WHERE myTable.thread = m.id) AS comments_count
FROM myTable m
Or Better to use LEFT JOIN
SELECT
m.id,
m.subject,
m.upvotes,
m.downvotes,
COUNT(mm.id) AS comments_count
FROM myTable m
LEFT JOIN myTable mm ON(mm.thread = m.id)
GROUP BY m.id
Related
I have 2 tables, articles and article_comments. Trying to get the comment count on the article listing page. However, any articles which do not have any comment under it is not being returned by my query.
SELECT `articles`.*, COUNT(comments.id) AS comment_count
FROM `articles` as `articles`
LEFT JOIN `article_comments` as `comments` ON `articles`.`id` = `comments`.`article_id`
How can I make the query return all rows from articles with comment_count as 0 if there are no comments for that post?
Obviously your query is missing a GROUP BY clause, that should enumerate all the columns from the articles table - or, if you are running MySQL with sql mode ONLY_FULL_GROUP_BY disabled, it shoul contain the primary key of articles.
It might be simpler to express this with a correlated subquery:
select
a.*,
coalesce(
(select count(*) from article_comments ac where ac.article_id = a.id),
0
) comments_count
from articles a
You can also pre-aggregate in a subquery:
select
a.*,
coalesce(c.comments_count, 0) comments_count
from articles a
left join (
select article_id, count(*) comments_count
from article_comments
group by article_id
) c on c.article_id = a.id
I have an articles table that and I am displaying it in the homepage in a while loop. Inside the while loop I want to display the comments count and images count for each article.
It is working for me now, but it is three queries in total, I am trying to combine it in the first query and then just display all of them in one while loop. Here is what I am trying to achieve:Articles page
The current format I am following:
//a represents articles table, c represents comments table, i represents image table
$query = mysqli_query($conn, "SELECT a.a_id, a.title, a.datetime, a.user_id FROM a ORDER BY a.datetime DESC");
while($fetch = mysqli_fetch_assoc($query){
$imageQ = msqli_query($conn, "SELECT COUNT(image_path), image_path FROM i WHERE a_id = '$fetch['a_id']'");
$imageFetch = mysqli_fetch_assoc($imageQ);
$commentQ = mysqli_query($conn, "SELECT COUNT(comment_id) FROM c WHERE a_id = '$fetch['a_id']'");
$commentFetch = mysqli_fetch_assoc($commentQ);
}
I want to cram all of these queries into one single query that fetches the article and comments count and image count for each article and the first image.
The images and comments are separate dimensions of the data. So, you have to be careful about how to bring them together. In your case, you can aggregate the values before doing the joins:
SELECT a.a_id, a.title, a.datetime, a.user_id,
i.num_images, c.num_comments
FROM a LEFT JOIN
(SELECT a_id, COUNT(image_path) as num_images
FROM i
GROUP BY a_id
) i
ON i.a_id = a.a_id LEFT JOIN
(SELECT a_id, COUNT(comment_id) as num_comments
FROM c
GROUP BY a_id
) c
ON c.a_id = a.a_id
ORDER BY a.datetime DESC;
You can use mysql nested queries something like
SELECT a.,tab1.,tab2.* FROM a INNER JOIN (SELECT * FROM b ) as tab1 INNER JOIN (SELECT * FROM c) as tab2
Hope this can get you to get desired output.
Thanks
I need a little help to count and group some MySql table records according of a range of dates given.
The table strcutre is this:
In advance, thank you for your help
This shoud give you the result that you need:
SELECT
d1.dt,
COUNT(DISTINCT t1.recid) AS departures,
COUNT(DISTINCT t2.recid) AS returns,
COUNT(DISTINCT t1.recid) + COUNT(DISTINCT t2.recid) AS total
FROM (SELECT departure_date AS dt FROM yourtable
UNION SELECT return_date FROM yourtable) d1
LEFT JOIN yourtable t1 ON d1.dt = t1.departure_date
LEFT JOIN yourtable t2 ON d1.dt = t2.return_date
GROUP BY
d1.dt
The first subquery will return all dates, not duplicated, present in your table (departures and returns).
I'm then trying to join each date to a departure date, using a LEFT JOIN. I'm then counting the DISTINCT t1.recid that make the join succeed..
I'm then trying to join each date to a return date, using LEFT JOIN. Total is the sum of both counts.
Fiddle is here.
I have two tables called 'events' and 'topics' each table can have many comments.
What I need to do is list all the events and topics with the amount of comments for each row. I've managed to return all the topics, which works great but I don't know how I can add the events table to the MySql. The comments and events table fields are listed below. Can anyone help me with this query?
Events:
ID
Event_Name
Comments:
post_id <-- the releated id for either the events or topics table
table <-- The table that the row belongs to so either topics or events
SELECT
t.id, t.title, c.created_at,
IF(ISNULL(c.allComments), 0, c.allComments) AS totalComments
FROM topics AS t
LEFT OUTER JOIN (
SELECT created_at, post_id, COUNT(*) AS allComments
FROM comments
GROUP BY post_id
) AS c ON c.post_id = t.id
ORDER BY tc.created_at DESC, c.allComments DESC
Sounds like events and topics should be the same table.
Still, I think we can do this with a UNION. Events and Topics have the same columns i hope? (Or at least the same important ones?)
(SELECT c.table as event_or_topic, e.*, count(C.table), MAX(C.created_at) as latest_c
FROM events E LEFT JOIN comments C on (C.post_id = E.id)
WHERE C.table = 'Events'
GROUP BY C.post_id)
UNION
(SELECT c.table as event_or_topic, t.id*, count(C.table), MAX(C.created_at) as latest_c
FROM topics T LEFT JOIN comments C on (C.post_id = E.id)
WHERE C.table = 'Topics'
GROUP BY C.post_id)
ORDER BY latest_c
Notice that the ORDER BY applies to the whole UNION, not the individual SELECTs.
The use of LEFT JOIN should allow those rows without Comments to still show. I think the problem is that we have parts of our select dependent on comments (ie - C.table, ordering on last comment, etc). The count should be fine - will just be zero if there are no comments.
You might need to change the SELECT part slightly. I'd like to display C.table so you know whether a row is a topic or event, but im afraid it might screw up the count. Do you need anything from comments besides the count? You use some columns other than post_id and table in your query that you neglected to explain in your question.
You still have columns I don't know what they are, like Comment's zoneTable
Try this:
SELECT
t.id, t.title, c.created_at, COUNT(c.allComments) AS totalComments
FROM topics AS t LEFT JOIN comments c ON t.id=c.post_id
GROUP BY t.id ORDER BY tc.created_at DESC, c.allComments DESC
If I understand your question you have 3 tables:
-Events
-Topics
-Comments
If that is true something like this should extract all the data:
SELECT *
FROM events,topics
LEFT JOIN comments ON post_ID = ID
ORDER BY date DESC
Hope i'm along the right lines!
W.
I've got it working. If anyone knows of a better and an efficient way of doing this, then please let me know:
(SELECT t.id, t.title, tc.dateCreated AS commentDate,
IF(ISNULL(tc.allComments), 0, tc.allComments) AS totalComments,
t.LastActive as dateChanged
FROM Events AS t
LEFT OUTER JOIN (
SELECT MAX(created_at) AS dateCreated, post_id,
COUNT(*) AS allComments
FROM comments
GROUP BY post_id
) AS tc ON tc.post_id = t.id)
UNION
(SELECT t.id, t.title, tc.dateCreated AS commentDate,
IF(ISNULL(tc.allComments), 0, tc.allComments) AS totalComments,
t.LastActive as dateChanged
FROM topics AS t
LEFT OUTER JOIN (
SELECT MAX(created_at) AS dateCreated, post_id,
COUNT(*) AS allComments
FROM comments
GROUP BY post_id
) AS tc ON tc.post_id = t.id)
ORDER BY commentDate DESC, dateChanged DESC, totalComments DESC
This is relating to my last question mysql query with AND, OR and NOT
Instead of editing the question, I am asking a new one because the question is only part of the previous question with an alteration.
I am looking to do a mysql query that returns me all articles that have all required topics.
Article
id
....
Topic
id
....
ArticleTopics
article_id
topic_id
type
something that would effectively do:
SELECT * FROM Article LEFT JOIN ArticleTopics ON Article.id = ArticleTopics.article_id
WHERE ArticleTopics.topic_id HAS ALL (these topics)
Is this possible? What is the best approach for this?
Several of the other answers suggest using aliases on the child table for each filter clause - this may not be very efficient or scale well.
Consider:
SELECT x.*
FROM Article x INNER JOIN
(SELECT t.article_id, COUNT(t.article_id)
FROM articleTopics t
WHERE t.topic_id IN ([your_list_of_topics])
GROUP BY t.article_id
HAVING COUNT(t.article_id)>=[number of elements in [your_list_of_topics]]
ORDER BY COUNT(t.article_id) DESC
LIMIT 0,100) AS ilv
ON x.id=ilv.article_id
Another advantage of this approach is that the structure of the query doesn't need to change with the number of topics you are searching for - you could even put them in a temporary table and perform a join instead of using the ' IN (...)' literal.
You'd need to try it out to see which query behaves better.
That is done using multiple joins with the same table.
To select all articles that have topics with ID 1, 2 and 3, you need to do:
SELECT * FROM Article a
INNER JOIN ArticleTopics at1 ON a.id = at1.article_id AND at1.topic_id = 1
INNER JOIN ArticleTopics at2 ON a.id = at2.article_id AND at2.topic_id = 2
INNER JOIN ArticleTopics at3 ON a.id = at3.article_id AND at3.topic_id = 3
// EDIT
Fixed it. Added table aliasses; I must have been working with good ORM solutions for too long...
SELECT
*
FROM
Article
WHERE
(SELECT COUNT(*) FROM ArticleTopics
WHERE Article.id = ArticleTopics.article_id AND
ArticleTopics.topic_id=1) > 0 AND
(SELECT COUNT(*) FROM ArticleTopics
WHERE Article.id = ArticleTopics.article_id AND
ArticleTopics.topic_id=2) > 0 AND
(SELECT COUNT(*) FROM ArticleTopics
WHERE Article.id = ArticleTopics.article_id AND
ArticleTopics.topic_id=3) > 0 AND
...