需要获取MySQL连接查询的计数

时间:2011-08-26 14:36:11

标签: mysql join count

查看具有相似标题的问题,并没有足够接近这个查询的工作。我知道这可能是相当简单的事情。

实际的查询是:

select entries.id, entry_id, user_id, person_id, campaign_id, mood_id, entries.created, deleted_date, entries.modified, entry_type, walking_hash 

 from entries, users 

where users.id = entries.user_id and users.is_deleted = 0 and entries.is_deleted = 0 

and entries.entry_type in ('xyz', 'abc', 'def', 'lmno')

为了得到计数我试过了:

select count(entries.id, entry_id, user_id, person_id, campaign_id, mood_id, entries.created, deleted_date, entries.modified, entry_type, walking_hash) 

from entries, users 

where users.id = entries.user_id and users.is_deleted = 0 and entries.is_deleted = 0 and entries.entry_type in ('xyz', 'abc', 'def', 'lmno')

但是会抛出非描述性错误并且不会运行。

3 个答案:

答案 0 :(得分:1)

你到底想要计算什么?只是行数?使用COUNT(*)。使用COUNT(column)计算此列中的所有非NULL值。

然后查询将只返回一行,即计数。

select count(*) as count
from entries, users 
where users.id = entries.user_id and users.is_deleted = 0 and entries.is_deleted = 0 
and entries.entry_type in ('xyz', 'abc', 'def', 'lmno')

答案 1 :(得分:0)

select count(1) 

from entries, users 

where users.id = entries.user_id and users.is_deleted = 0 and entries.is_deleted = 0 and entries.entry_type in ('xyz', 'abc', 'def', 'lmno')

答案 2 :(得分:0)

你应该使用简单的

Select count(entries.id)  ....