对SubQuery中的多个值重复主查询

时间:2012-10-17 06:39:58

标签: mysql

我的桌子是

 login_table
    {
    varchar: User_id//primary key
    //other stuff
    }
   token_table
   {
    varchar token_table;//primary key
    varchar user_id
   }
   token_messages
   {varchar: Mes_id
   varchar: token_id;
    }

现在我希望得到一些用户下每个令牌的消息数量; 喜欢:

token_id Mes_count
1          5
5          12
6          0
7          4 

这里1 5 6 7是say user:deepu

的标记

2 个答案:

答案 0 :(得分:0)

试试这个:

 SELECT tm.token_id, COUNT(t.user_id) `Mes_count`
 FROM token_messages tm
 INNER JOIN token_table t ON tm.token_id = t.token_id
 INNER JOIN login_table l ON t.user_id = l.user_id
 WHERE l.user_name = 'deepu'
 GROUP BY tm.token_id

答案 1 :(得分:0)

试试这个:

select tm.token_id,count(*) as msg_cnt from 
token_messages tm inner join token_table t
on tm.token_id=t.token_id and t.user_id in(select user_id from login_table where user_name = 'deepu')
group by tm.token_id