using COUNT(field) result in other function

This doesn't work:



AW


SELECT c.name, cc.camp_id, cc.week_num, COUNT(cc.camp_id) AS cnt, cw.camp_size,
IF(cnt >= camp_size,1,0) AS is_full
 
// I have to use the count() function again:
 
SELECT c.name, cc.camp_id, cc.week_num, COUNT(cc.camp_id) AS cnt, cw.camp_size,
IF(COUNT(cc.camp_id)  >= camp_size,1,0) AS is_full
 
// Is there no way around using the same COUNT(cc.camp_id)  twice?

Open in new window

LVL 1
drew22Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Cornelia YoderArtistCommented:
Have  you tried this:

SELECT c.name, cc.camp_id, cc.week_num, COUNT(cc.camp_id) AS cnt, cw.camp_size,
IF( cnt  >= camp_size,1,0) AS is_full
0
drew22Author Commented:
yes, that's my first example and it doesn't work:  #1054 - Unknown column 'cnt' in 'field list'
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
here we go:
SELECT c.name, cc.camp_id, cc.week_num, COUNT(cc.camp_id) AS cnt, cw.camp_size,
CASE WHEN COUNT(cc.camp_id) >= camp_size THEN 1 ELSE 0 END AS is_full

Open in new window

0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Cornelia YoderArtistCommented:
Then you'll probably need to use a CASE statement instead of the IF
0
drew22Author Commented:
My second example works, but  I was looking to avoid using the same COUNT(cc.camp_id)  twice. I'm sure MySQL is smart enough not to do the COUNT twice, so maybe it doesn't matter.
0
ee_rleeCommented:
SELECT T.*, IF(T.cnt  >= camp_size,1,0) AS is_full
FROM (SELECT c.name, cc.camp_id, cc.week_num, COUNT(cc.camp_id) AS cnt, cw.camp_size FROM ....) T
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.