2012-04-30 63 views
1

我有两个表,我想从两个表中计数。从第一个表格计数然后从第二个表格计数,结果应该是这样的。来自多个表的多个计数

Count(users.name) Count(users_types) 
     5     8 

但我的查询带来这样的结果

Count(users.name) Count(users_types) 
     8     8 

这里是我的查询

select count(users.users), 
count(users_types.users_types) 
form users , users_types 

我怎样才能得到正确的结果呢?

回答

0

使用子查询这样

select 
    count(users.users) Users, 
    (select 
    count(users_types.users_types) 
    from teams) UsersTypes 
from users 
1

尝试:


select 
(select count(users.users) from users), 
(select count(users_types.users_types) form users_types) 
+0

这工作得很好,但减慢显著当表中包含大量数据的查询。原来的问题似乎希望在没有速度问题的情况下完成。 – Nightwolf

+0

好吧,在这个问题中没有提到这个问题..! –

+0

答案没有错,但为什么不添加允许更快结果的备选答案。 – Nightwolf

1
select (select count(*) cnt1 from table1), 
(select count(*) cnt2 from table2)