我在这里面临一个问题,我需要进行如下的查询:
select client.name, client18n.display_name
FROM Client client
JOIN TEAM team on (client.id = team.client_id)
JOIN TEAM_KGK_USER members on (members.team_members_id = team.id)
JOIN USER users on (users.id = members.user_id
JOIN CLIENT_I18N client18n on (client.id = client18n.client_id)
Where users.id = @userId
查询的主要思想是,我需要返回给定用户ID的所有客户端,因为用户是多个团队的一部分,并且每个团队都有一个拥有该团队的客户端。但是我无法返回重复的客户端,所以我试图使用distinct,但是我遇到的问题很少:
我正在使用Oracle数据库,在这里说明我的问题:
想象一下用户:John是团队A,团队B,团队C和团队D的成员
所以这些团队的各自客户是:
A队:ClientA B队:ClientA C队:ClientB D队:ClientC
对于客户端,他们在ClientI18N中有多个值:
ClientA:ClientA_US ClientA:ClientA_FR
ClientB:ClientB_US ClientB:ClientB_FR
继续......
所以如果我找用户John
即使我使用distinct,也会为我返回两次ClientA,因为我将区别于client.name和client18n.display_name。
我尝试返回的不仅仅是client.name,因为它是一个唯一值,但我需要通过client18n.display_name订购,因为如果我不知道偏移量不会起作用。
这是我在运行查询时得到的结果:
NAME DISPLAY_NAME
Client_38276QVAAU3 GTA_38276QVAAU3
Client_38276QVAAU GTA_38276QVAAU
Client_38276QVAAU el cliente!
Client_38276QVAAU3 der client!
Client_38276QVAAU2 le client!
Client_38276QVAAU2 GTA_38276QVAAU2
正如您所见,即使我使用distinct也会返回重复的客户名称
答案 0 :(得分:0)
试试这个:
select client.name, client18n.display_name
from (select client.name, client18n.display_name
FROM Client client
JOIN TEAM team on (client.id = team.client_id)
JOIN TEAM_KGK_USER members on (members.team_members_id = team.id)
JOIN USER users on (users.id = members.user_id
JOIN CLIENT_I18N client18n on (client.id = client18n.client_id)
Where users.id = @userId
order by client18n.display_name)
group by client.name
答案 1 :(得分:0)
所以最后我找到了这个解决方案,使用了union
select client.name,client18n.display_name ,client18n.locale_id
FROM Client client
JOIN TEAM team on (client.id = team.client_id)
JOIN TEAM_USER members on (members.team_members_id = team.id)
JOIN USER users on (users.id = members.user_id)
JOIN CLIENT_I18N client18n on (client.id = client18n.client_id)
where client.name not in ( Select distinct cli.name
from CLIENT cli
where client18n.locale_id in ('en_US','fr_FR') group by cli.name having COUNT(*) > 1) and client18n.locale_id in ('en_US','fr_FR')
union Select c.name,i18n.display_name ,i18n.locale_id
FROM Client c
JOIN CLIENT_I18N i18n on (c.id = i18n.client_id)
JOIN TEAM t on (c.id = t.client_id)
JOIN TEAM_USER tu on (tu.team_members_id = t.id)
JOIN USER u on (u.id = tu.user_id)
where i18n.locale_id = 'fr_FR'