[thelist] count() multiple columns in a single query?

Jeremy Weiss eccentric.one at gmail.com
Thu Dec 13 12:35:54 CST 2007


Okay, I've inherited a script that has two Select queries in it and I'm
trying to merge them. I'm working in PHP and MySQL. Overall I'm checking to
see if there are any open leads, then from the open leads I'm checking to
see if they've been claimed (received) by any of the salespeople. Since it's
possible that there could be multiple leads for the same client over time,
I'm just counting open records. If count is > 0 then I echo an alert. If
there are open leads that someone has claimed I echo a different alert.

Script 1 - looks for records with nothing in the closed column
Select COUNT(*) FROM followup WHERE fk_id=$id AND ((closed IS NULL) OR
(closed = ''))

Script 2 - looks for records from the same records set as above for records
with nothing in the received column
Select COUNT(*) FROM followup WHERE fk_id=$id AND received_by != '' AND
((closed IS NULL) OR (closed = ''))

What I've managed so far....
Select COUNT(received_by) FROM (Select COUNT(closed) FROM followup as
tblclosed WHERE fk_id=$id AND ((closed IS NULL) OR (closed = ''))) as
tblreceived WHERE received_by != ""

I added the 'as' statements in because it was telling me "Every derived
table must have its own alias." But with them in, it's now telling me
"Unknown column 'received_by' in 'where clause'"

If someone could point me in the right direction I'd appreciate it.

Thanks.
-jeremy



More information about the thelist mailing list