userName | email(p.k) | accountType(p.k)
Vivek | [email protected] | google
Rahul | [email protected] | facebook
Rohit | [email protected] | google
eventId(p.k) | email(f.k) | accountType(f.k) | eventName
1 | [email protected] | google | birthday
eventId(f.k) | email(f.k) | accountType(f.k) | contribute
1 | [email protected] | facebook | $20
1 | [email protected] | google | $30
我想要结果数据
userName contribute name count eventName
Rahul $20 Vivek 2 birthday
Rohit $30 Vivek 2 birthday
rahul contribute $20 for vivek birthday 2 contributer are there for this event
rohit contribute $30 for vivek birthday 2 contributer are there for this event
查询我使用的内容
SELECT
uc.userName as cName,
uc.email as cEmail,
uc.accountType as cAccType,
contribute,
u.userName as userName,
u.email as email,
u.accountType as accountType,
eventName
FROM user u
join event e on u.email=e.email and u.accountType=e.accountType
join contributer c on c.eventId=e.eventId
join user uc on c.email=uc.email and c.accountType=uc.accountType
还有一些这样的事情
SELECT *
FROM user
JOIN contribute ON user.email = contribute.email
JOIN event ON event.id = contribute._eventId
JOIN user u2 ON event.email = u2.email;
如何在同一查询中获取带有计数的数据,任何人都可以帮助我
当用户在结果中出现两次时,您必须使用该表两次 - 一次作为贡献者,另一次作为庆祝活动的人。
像这样的东西(Oracle):
SQL> with t_user (username, email, accounttype) as
2 (select 'vivek', '[email protected]', 'google' from dual union
3 select 'rahul', '[email protected]', 'facebook' from dual union
4 select 'rohit', '[email protected]', 'google' from dual
5 ),
6 t_event (eventid, email, accounttype, eventname) as
7 (select 1, '[email protected]', 'google', 'birthday' from dual),
8 t_contributer (eventid, email, accounttype, contribute) as
9 (select 1, '[email protected]', 'facebook', '$20' from dual union
10 select 1, '[email protected]', 'google' , '$30' from dual)
11 select
12 t1.username || ' contribute ' || c.contribute ||
13 ' for ' || t2.username ||' '|| e.eventname ||' ' ||
14 count(*) over (partition by c.eventid order by null) ||
15 ' contributer for this week' result
16 from t_contributer c join t_event e on c.eventid = e.eventid
17 join t_user t1 on t1.email = c.email
18 join t_user t2 on t2.email = e.email;
RESULT
--------------------------------------------------------------------------------
rahul contribute $20 for vivek birthday 2 contributer for this week
rohit contribute $30 for vivek birthday 2 contributer for this week
SQL>
使用提示:
select user_name,(select contribute from contributer
where email = usr.email) as contribute,(select user_name from user_table where email = (select email
from event_table where event_id = (select event_id from contributer where email = usr.email))) as name,(select count(event_id) from contributer
where event_id = (select event_id from contributer
where email = usr.email)) as count from user_table as usr
结果:
userName contribute name count
Vivek NULL NULL 0
Rahul $20 Vivek 2
Rohit $30 Vivek 2
通过添加(select event(eventId)来自参与者,其中eventId = e.eventId)它帮助了我很多。
SELECT
uc.userName as cName,
uc.email as cEmail,
uc.accountType as cAccType,
c.contribute,
u.userName as userName,
u.email as email,
u.accountType as accountType,
e.eventName ,
(select count(eventId) from contributor where eventId = e.eventId)
FROM user u
join event e on u.email=e.email and u.accountType=e.accountType
join contributer c on c.eventId=e.eventId
join user uc on c.email=uc.email and c.accountType=uc.accountType