我们正在开发类似社交网站的东西。我有任务要做 “跟我来”功能。在我们的网站中,对象是用户,团队,公司,渠道和团体(请不要问为什么有团队和团队 - 这对我来说也很复杂,但团队与用户的才能相关)
用户,团队,渠道,公司和团体都拥有自己的桌子。
我有一个查询,让我这样的所有追随者的领导者
select
--fo.leader_id,
--fo.leader_type,
us.name as user_name,
co.name as company_name,
ch.title as channel_name,
gr.name as group_name,
tt.name as team_name
from
follow_up fo
left join users us
on (fo.leader_id = us.id and fo.leader_type = 'user')
left join companies co
on (fo.leader_id = co.user_id and fo.leader_type = 'company')
left join channels ch
on (fo.leader_id = ch.id and fo.leader_type = 'channel')
left join groups gr
on (fo.leader_id = gr.id and fo.leader_type = 'group')
left join talent_teams tt
on (fo.leader_id = tt.id and fo.leader_type = 'team')
where
follower_id = 83
我需要获得所有字段:
作为SELECT产品中的一个字段。 我试图将它们的所有相同名称命名为“名称”,但Oracle对其进行了编号。 请帮助:)
答案 0 :(得分:3)
我不确定为什么你需要将它们作为一个字段,因为你不需要在客户端分割信息吗?无论如何,你能做到的一种方式就是这样:
user_name || '|' || company_name || '|' || channel_name || '|' || group_name || '|' || team_name all_fields
这会给你一个名为all_fields的管道分隔字段。如果您有来自不同表的多个user_name字段,则可以使用相同的方法:
table1.user_name || '|' || table2.user_name ... all_user_names
然后您可以在客户端分割字段。
就个人而言,我会做这样的事情:
table1.user_name table1_user_name
, table2.user_name table2_user_name
...
换句话说,只需为每个user_name使用唯一的列别名。
答案 1 :(得分:1)
查询结果集中的列名必须是唯一的。对于给定的关注者,您可能希望为每个用户,公司,渠道,小组和团队分配一行?在这种情况下,我会使用这样的查询:
select fo.leader_type, us.name
from follow_up fo
join users us
on (fo.leader_id = us.id and fo.leader_type = 'user')
where follower_id = 83
UNION ALL
select fo.leader_type, co.name
from follow_up fo
join companies co
on (fo.leader_id = co.user_id and fo.leader_type = 'company')
where follower_id = 83
UNION ALL
select fo.leader_type, ch.title as name
from follow_up fo
join channels ch
on (fo.leader_id = ch.id and fo.leader_type = 'channel')
where follower_id = 83
UNION ALL
select fo.leader_type, gr.name
from follow_up fo
join groups gr
on (fo.leader_id = gr.id and fo.leader_type = 'group')
where follower_id = 83
UNION ALL
select fo.leader_type, tt.name
from follow_up fo
join talent_teams tt
on (fo.leader_id = tt.id and fo.leader_type = 'team')
where follower_id = 83
答案 2 :(得分:0)
我想到了,我想出了这个解决方案:
它比Jeffrey Kemp的解决方案慢吗?
select
fo.leader_id,
fo.leader_type,
case
when us.subdomain is not null then us.subdomain
when us2.subdomain is not null then us2.subdomain
--when co.name is not null then co.name
when ch.service_url is not null then ch.service_url
when gr.id is not null then to_char(gr.id)
when tt.subdomain is not null then tt.subdomain
else 'nothing!'
end
as leader_url,
case
when us.name is not null then us.name
when co.name is not null then co.name
when ch.title is not null then ch.title
when gr.name is not null then gr.name
when tt.name is not null then tt.name
else 'nothing!'
end
as leader_names,
case
when us.img_avatar_path is not null then us.img_avatar_path
when us2.img_avatar_path is not null then us2.img_avatar_path
--when us.img_avatar_path is not null and fo.leader_id = co.user_id and fo.leader_type = 'company' then us.img_avatar_path
when ch.default_img is not null then ch.default_img
when gr.img_avatar_path is not null then gr.img_avatar_path
when tt.img_avatar_path is not null then tt.img_avatar_path
else 'nothing!'
end
as img_avatar_path,
case
when us.img_avatar_x is not null then us.img_avatar_x
when us2.img_avatar_x is not null then us2.img_avatar_x
when ch.default_img_x is not null then ch.default_img_x
when gr.img_avatar_x is not null then gr.img_avatar_x
when tt.img_avatar_x is not null then tt.img_avatar_x
else 0
end
as img_avatar_x,
case
when us.img_avatar_y is not null then us.img_avatar_y
when us2.img_avatar_y is not null then us2.img_avatar_y
when ch.default_img_y is not null then ch.default_img_y
when gr.img_avatar_y is not null then gr.img_avatar_y
when tt.img_avatar_y is not null then tt.img_avatar_y
else 0
end
as img_avatar_y
from
follow_up fo
left join users us
on (fo.leader_id = us.id and fo.leader_type = 'user')
left join companies co
on (fo.leader_id = co.user_id and fo.leader_type = 'company')
left join users us2
on (co.user_id = us2.id)
left join channels ch
on (fo.leader_id = ch.id and fo.leader_type = 'channel')
left join groups gr
on (fo.leader_id = gr.id and fo.leader_type = 'group')
left join talent_teams tt
on (fo.leader_id = tt.id and fo.leader_type = 'team')
where
follower_id = :follower_id