Merge 2 different tables and display content sorting by number of views (larger to smaller) of the two joints in Mysql

Asked

Viewed 99 times

4

Whereas I have 2 tables in the comic, "news" and "news", each with its own fields:

inserir a descrição da imagem aqui

It is possible to make a SELECT in both tables at the same time so that I return the 4 highest values of the "printed" fields (table NOTICIAS) and "clicks" (table NOVIDADES), the two combined?

The expected result would be this (ordering from highest to lowest):

  1. Wrong (15 views)
  2. Hello (9 views)
  3. Medium (6 views)
  4. Good (5 views)
  • Tables have no foreign key relation??

  • @Diegofelipe I’m sorry, but I don’t understand your question.

  • the news id is the same news id?

  • If there is any link between the tables, if one has a key field of the other.

  • @Marllonnasser The tables have their own Ids and one has no relation to the other.

1 answer

4


select * from 
(select titulo, impressoes as visualizacoes
from 
noticias 
union ALL
select titulo, cliques as visualizacoes
from cliques) result order by result.visualizacoes desc
limit 0,4

Upshot:

inserir a descrição da imagem aqui

Fiddle

  • He only wants the big four, I think a simple addition to his code with limit 1,4 resolve.

  • limit 0,4. I edited :)

  • It’s true, 0.4 kkk

  • @Marllonnasser It worked perfectly! Mt thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.