userName | email(p.k) | accountType(p.k)
Vivek | v@asd.asd | google
Rahul | r@asd.asd | facebook
Rohit | r@amd.asd | google
eventId(p.k) | email(f.k) | accountType(f.k) | eventName
1 | v@asd.asd | google | birthday
eventId(f.k) | email(f.k) | accountType(f.k) | contribute
1 | r@asd.asd | facebook | $20
1 | r@amd.asd | 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;
如何在同一个查询中同时获取带有计数的两个数据,任何人都可以帮助我
答案 0 :(得分:0)
使用子查询:
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
答案 1 :(得分:0)
由于用户在结果中出现两次,因此您必须使用该表两次-一次作为贡献者,另一次作为在庆祝活动中受到嘉奖的人。
类似这样的东西(Oracle):
SQL> with t_user (username, email, accounttype) as
2 (select 'vivek', 'v@asd.asd', 'google' from dual union
3 select 'rahul', 'r@asd.asd', 'facebook' from dual union
4 select 'rohit', 'r@amd.asd', 'google' from dual
5 ),
6 t_event (eventid, email, accounttype, eventname) as
7 (select 1, 'v@asd.asd', 'google', 'birthday' from dual),
8 t_contributer (eventid, email, accounttype, contribute) as
9 (select 1, 'r@asd.asd', 'facebook', '$20' from dual union
10 select 1, 'r@amd.asd', '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>
答案 2 :(得分:0)
通过添加(从贡献者中选择count(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