Need to get a Count on a MySQL join query
Looked through the questions with similar titles and nothing was close enough to get this query working. I know this is probably something fairly simple though.
The actual query is this:
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')
To get the count I tried this:
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')
but that throws a non-descript error and doesn't run.
What exactly do you want to count? just the number of rows? use COUNT(*)
. use COUNT(column)
to count all non-NULL values in this column.
The query will then only return a single row, the count.
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')
You should use simple
Select count(entries.id) ....
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')
精彩评论