SQL - Use of Distinct

Asked

Viewed 102 times

0

Friends need a Select that loads: (all portals linked to a user and an additional column informing which portals were selected in a given news)

So far my SQL is like this...:

select 
   distinct 
   case when usudesc.txt_nome is null then 0 else 1 end as MeusPortais,
   case when nopo.id_noticia is null then 0 else 1 end as PotalSelecionado,
   porti.id_portal, nopo.id_noticia, porti.txt_apelido
from tb_portal porti 
   left join tb_noticiasportais nopo on nopo.id_portal = porti.id_portal 
       and nopo.id_noticia = '26' 
   left join tb_usuarioportal usu on usu.id_portal = porti.id_portal 
       and usu.id_usuario = '1' 
   left join tb_usuario usudesc on usudesc.id_usuario = usu.id_usuario
   left join tb_noticias noti on noti.id_noticia = nopo.id_noticia
order by porti.txt_apelido asc

The base has this structure:

tb_noticias(id_noticia, txt_titulo)


tb_noticiasportais (id_noticia, id_portal)


tb_portal (id_portal, txt_titulo)


tb_user portal(id_usuario, id_portal)


tb_usuario(id_usuario, txt_usuario)

2 answers

1

RESOLVED:

select * from (
    select 
    distinct 
    case when usudesc.txt_nome is null then 0 else 1 end as MeusPortais,
    case when nopo.id_noticia is null then 0 else 1 end as PotalSelecionado,
    porti.id_portal, nopo.id_noticia, porti.txt_apelido
from tb_portal porti 
    left join tb_noticiasportais nopo on nopo.id_portal = porti.id_portal 
    and nopo.id_noticia = '26' 
    left join tb_usuarioportal usu on usu.id_portal = porti.id_portal 
    and usu.id_usuario = '1' 
    left join tb_usuario usudesc on usudesc.id_usuario = usu.id_usuario
    left join tb_noticias noti on noti.id_noticia = nopo.id_noticia
) as U
where U.MeusPortais = '1'

0

select 
porti.id_portal, nopo.id_noticia, porti.txt_apelido, usudesc.txt_usuario, noti.txt_titulo
from tb_portal porti 
left join tb_noticiasportais nopo on nopo.id_portal = porti.id_portal 
and nopo.id_noticia = 26 
left join tb_usuarioportal usu on usu.id_portal = porti.id_portal 
left join tb_usuario usudesc on usudesc.id_usuario = usu.id_usuario
left join tb_noticias noti on noti.id_noticia = nopo.id_noticia
order by porti.txt_apelido asc
  • Friend... it’s not quite that. But it gave me a line of reasoning:

Browser other questions tagged

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