B
B
birdy902019-10-23 13:33:57
PHP
birdy90, 2019-10-23 13:33:57

How to form a news/events feed from several tables (php/mysql)?

Good afternoon!
I understand that the question has little to do with the specified technologies, but just in case, I indicated them.
I'm trying to generate a news feed. This feed includes news, articles, videos, reports. Each of these data types is in a separate table. At least because they may have a slightly different set of fields (the main ones are the same), each of them has its own categorization.
The simplest thing is to select everything through union, then sort and select the ones you need:

select * from 
    (select * from news 
        union 
    select * from articles) as materials
order by time desc
limit <deisred_offset>, 10

But in this version, it confuses me that the tables are quite large. Firstly, the selection and sorting will take time, and secondly, offset, as far as I know, the farther from the beginning of the selection, the worse.
On the other hand, nothing more optimal comes to my mind:
- so that it is sorted predictably by dates ( after all, news can be published many times a day, and articles, for example, once every couple of days. Or in general, one of the categories has not been published for a long time. This means that the limit on `select` to `union` cannot be set
- to be able to do normal paging. You can focus on the identifier, but in this case the identifiers intersect, and you cannot look at them. And if by date, then we return to the previous point - how to choose data that can be published very often, or not published for years
. Are there any guidelines / practices for building such structures?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
zhaar, 2019-10-23
@zhaar

select top 10 * from news1 order by postdate desc
union all
select top 10 * from articles1 order by postdate desc
union all
...
This is the easiest option.
Screw here the conditions for sampling for each article (cut-off date, author, etc.) and you will be happy

K
Kirill Gorelov, 2019-10-23
@Kirill-Gorelov

To make on each table separately on one request.
I don't think it's worth messing around with union.
First, you can make your request even "heavier".
Secondly, I do not think that you have a very large load there, that you have to pervert with the combination of requests.
Well, the last thing, since it comes to that, then do it according to the principle of "fat client". On js, make a request to get the latest news separately, as an example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question