I have two tables, with lists of all news (news title, news content) and a table with news comments. On the main page, you need to display the title, content and number of comments on the news. Name and content brought out, tk. data are in the same table (displayed in the for loop), and there were problems with the output of the number of comments, I don’t know how to display them. It turns out that you need to display the number of comments corresponding to this article. How to do this, tell me?
Answer 1, authority 100%
It is possible to get all the fields from the news table and the number of comments in one request. Something like:
SELECT news.*, COUNT(comments.*) AS count_comments FROM news LEFT JOIN comments ON comments.id_news = news.id_news GROUP BY 1