Easy... but something is wrong?!!?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Easy... but something is wrong?!!?

Post by seodevhead »

Why is this returning the wrong COUNT of table1?

'table1' has a total of 5 records.

Query:
"SELECT COUNT(table1.id) FROM table1, table2 WHERE registered='Y'";

Note: registered is a column in table2.

This query is returning the COUNT as 29 records??? But there are only 5 records total in table1??? How could this be? Thanks!
RobertPaul
Forum Contributor
Posts: 122
Joined: Sun Sep 18, 2005 8:54 pm
Location: OCNY

Post by RobertPaul »

If all you want is the record count of table1 then why not just do SELECT COUNT(*) from table1 ... AFAIK you're not (usually) supposed to put anyththing in COUNT() other than an asterisk. Otherwise things get funky. That's obviously not the technical explaination but it escapes me at the moment...
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post by seodevhead »

Well... in a sense all I really want is records from table1... but I had to include table2 so I could add 'register' in the WHERE clause.
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post by seodevhead »

[SOLVED] I'm just going to use mysql_num_rows().
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: Easy... but something is wrong?!!?

Post by timvw »

seodevhead wrote:Why is this returning the wrong COUNT of table1?

'table1' has a total of 5 records.

Query:
"SELECT COUNT(table1.id) FROM table1, table2 WHERE registered='Y'";

Note: registered is a column in table2.

This query is returning the COUNT as 29 records??? But there are only 5 records total in table1??? How could this be? Thanks!
Do you know what happens when you select from more than one table?
Do you know what happens if you specifiy a column as argument in an aggregate function?

If not, it's up to you to do some research.
Post Reply