加入表格以引入另一个字段

时间:2013-01-22 20:03:18

标签: sql oracle

我目前有这个查询:

select f.chainid,count(f.player_uuid) as Favorites
from deals_player_favorite f
group by f.chainid
order by 2 desc

结果是:

CHAINID            FAVORITES
25                 2771
2207               2282
3940               1954

等...

我有另一个名为deals_deals的表,它还包含CHAINID字段。从这个表中,我想加入一个名为VENUE in的字段,这样每个CHAIN ID都有一个Venue描述,输出看起来像这样

CHAINID    VENUE           FAVORITES    
25        Amazon.com        2771
2207       Walmart          2282
3940       CVS              1954

等...

如何将场地字段正确加入查询,使用CHAIN ID作为deals_deals表和deals_player_favorite表中的键。

我尝试了内部联接,导致结果太多。

deals_deals表的字段为CHAINID和VENUE ..

deals_player_favorite表的字段为CHAINID和PLAYER_UUID,但不包括deals_deals表的所有CHAINID,只包含player_uuid访问过的那些CHAINID。

示例数据:

deals_deals表

VENUE        CHAINID
Walmart         235
Aeropostale     1467
Checker's       881

deals_player_favorite table

PLAYER_UUID         CHAINID
23rjior23-32fjdf     235

请记住,deals_player_favorite仅包含已点击的特定CHAINID,而非所有链接....

1 个答案:

答案 0 :(得分:6)

SELECT F.chainid, V.Venue, COUNT(f.player_uuid) as Favorites
FROM deals_player_favorite F
INNER JOIN Venues V
    ON F.chainid = V.chainid
GROUP BY F.chainid, V.Venue
ORDER BY COUNT(f.player_uuid) DESC