0

我的桌子是

 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 是说用户的令牌:deepu

4

2 回答 2

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
于 2012-10-17T06:43:25.913 回答
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
于 2012-10-17T07:14:12.600 回答