[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

using COUNT(field) result in other function

Posted on 2008-02-10
6
Medium Priority
?
645 Views
Last Modified: 2008-03-08
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

0
Comment
Question by:drew22
6 Comments
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 20861970
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
 
LVL 1

Author Comment

by:drew22
ID: 20862016
yes, that's my first example and it doesn't work:  #1054 - Unknown column 'cnt' in 'field list'
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20862062
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 20862068
Then you'll probably need to use a CASE statement instead of the IF
0
 
LVL 1

Author Comment

by:drew22
ID: 20862116
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
 
LVL 26

Accepted Solution

by:
ee_rlee earned 2000 total points
ID: 20865959
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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question