Hey.
To do that, you could use either a
JOIN or a
Subquery.
For example, say I had a News table and a NewComment table, where the comments were link to the news by a Foreign Key on the News id column.
Using a Subquery, I could get the news info and count the number of comments for it by doing:
-
SELECT
-
n.`id`, n.`headline`, n.`body`, n.`author`,
-
(SELECT COUNT(*)
-
FROM `news_comments` AS nc
-
WHERE nc.`news_id` = n.`id`
-
) AS 'comments'
-
FROM `news` as n
-