V
V
Vitaly2018-09-12 18:22:25
Laravel
Vitaly, 2018-09-12 18:22:25

How to join two tables correctly?

There are two tables containing news for different sports:
soccer_news and hockey_news They
are not connected in any way.
You need to connect these two tables and sort by date, then display them through pagination.
Thought to do something like this:

$soccer =  App\SoccerNews::get();
$hockey =  App\HockeyNews::get();
$collection  = new Collection;
$all = $collection->merge($soccer)->merge($hockey);

But there can be a million articles... which means that you need too many operatives to implement all this...
Or will you have to change the structure of the database and write everything in one all_news table?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alexalexes, 2018-09-12
@kiukishenkaec

It is better if you have all types of news in one table, and differ by section_id (respectively, 0 - no topic, 1 - football, 2 - hockey, etc.)
Then the request for a portioned selection is elementary.

select a.*
  from (select n.*,
              rank() over (partition by n.section_id order by n.publication_date desc) rnk -- нумеруем порядок новости в пределах каждой секции по порядку даты публикации
            from news n) a
where a.rnk < 10 -- сколько новостей выводить на каждую секцию
order by a.section_id, a.publication_date desc

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question