mountaingirl
mountaingirl

Reputation: 248

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.

Upvotes: 0

Views: 155

Answers (3)

knittl
knittl

Reputation: 265231

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')

Upvotes: 1

Andrej
Andrej

Reputation: 7504

You should use simple

Select count(entries.id)  ....

Upvotes: 0

Vadim Samokhin
Vadim Samokhin

Reputation: 3456

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')

Upvotes: 0

Related Questions