tags:

views:

28

answers:

2

Here is a table i have

CREATE TABLE `CUSTOMERSTATUSTYPES` (
  `CustomerStatusId` int(1) unsigned NOT NULL auto_increment,
  `CustomerStatusName` enum('ACTIVE','SUSPEND','TERMINATE','CANCEL') default NULL,
  PRIMARY KEY  (`CustomerStatusId`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

When i write

GROUP BY ... WHERE cst.CustomerStatusName<>'TERMINATE' ;

i get a syntax error

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE cst.CustomerStatusName<>'TERMINATE'' at line 1

How do i correctly check this?

+3  A: 

The WHERE clause must come before the GROUP BY clause.

Greg
+1  A: 

You either need to put the WHERE clause before the GROUP BY clause, or you need to use the HAVING keyword.

See here for more info:

http://www.databasejournal.com/features/mysql/article.php/3469351/The-HAVING-and-GROUP-BY-SQL-clauses.htm

Phil