选择并统计表关系?

时间:2010-01-15 09:15:10

标签: mysql

我有03张关系表

jos_session
 +userid

jos_gj_user
  +id_user
  +id_group

jos_gj_jb
  +group_id
  +category_id

我想基于表jos_gj_jb中的category_id来计算表jos_session中的用户

这就是我正在尝试的

 SELECT COUNT(s.userid) 
 FROM jos_session s 
 WHERE s.user.id IN (SELECT id_user 
                     FROM jos_gj_user
                     WHERE id_group(SELECT group_id FROM jos_gj_jb WHERE category_id= .$variable.))

如何为category_id计算user_id?

2 个答案:

答案 0 :(得分:1)

虽然我不完全确定我是否理解你的问题,但这是我的建议:

SELECT COUNT( DINSTINCT id_user )
FROM jos_gj_user INNER JOIN jos_gj_jb ON id_group = group_id
WHERE category_id= $variable

此查询将返回与$variable中的值引用的类别相关的一个或我的组成员的不同用户的数量。

答案 1 :(得分:-1)

 SELECT COUNT( DINSTINCT id_user )
 FROM jos_session INNER JOIN jos_gj_user ON id_user = userid INNER JOIN jos_gj_group ON id_group = group_id
 WHERE category_id= $variable GROUP BY category_id