When you need to keep querying in a repeat statement

Asked 2 years ago, Updated 2 years ago, 32 views

There are two tables, A and B.
A is a post, and B is a comment on the post.

Like a typical bulletin board, a list of posts is printed, and I want to print a list of comments at the bottom of each list as below.
I don't think I'm turning queries that keep bringing up the list of comments in the list of posts.
If this is the case, I would like to ask you what would be the right method.

1 text title

        The first comment 
        The second comment 

Two-letter title 

        The first comment 
        The second comment 

The title of the 3rd sentence 

        The first comment 
        The second comment

mysql

2022-09-21 19:34

1 Answers

Um... Can't we just do JOIN and put a limit and bring it all and share it appropriately?

SELECT `p`.*, `c`.`id` as `cid`, `c`.`body` as `comment`
FROM `post` AS `p`
  LEFT JOIN `comment` AS `c` ON (`p`.`id` = `c`.`post_id`)
LIMIT 50;

demo


2022-09-21 19:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.