D
D
driverx182020-03-25 14:41:59
React
driverx18, 2020-03-25 14:41:59

How to select for each article 5 comments in one selection?

You need to select 5 comments for each article. Those. if article 44 has, say, 10 comments, then only 5 should be returned for this article in the sample. And so for each article in the sample. It is clear that simply LIMIT 5 at the end will not help, and will simply return the first 5 comments, regardless of the article. As I understand it, you need to do some kind of subquery?

SELECT `c`.*
FROM comments AS `c` WHERE `c`.`articleId` IN (
     '44',
     '55',
     '414',
     '555',
     '775'
) AND `c`.`approved` ='1' ORDER BY `c`.`date` DESC;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Afanasy Zakharov, 2019-09-03
@afanasiyz

I'm not going to sell it for you for free, I'm sorry. I can try to give a couple of tips
. The layout of man and woman should be universal, this will be the main react component. You will call this component with the sex prop (there will be a floor), and when the component is rendered, you will need to make a request to the backend for the necessary goods. Filling with a list of products should also be done not through innerHTML, but using react tools, through state.

M
Maxim, 2020-03-25
@MaximaXXl

If you showed here a table and a bunch in which these "comments" are located, it would be easier. And so...

select *
from (SELECT `c`.*, row_number() OVER (partition by `c`.`articleId` order by `c`.`date` DESC ) rn
FROM comments AS `c` WHERE `c`.`articleId` IN (
     '44',
     '55',
     '414',
     '555',
     '775'
) AND `c`.`approved` ='1' ORDER BY `c`.`date` DESC) t1 
where rn <= 5;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question