This post originated from an RSS feed registered with Ruby Buzz
by Red Handed.
Original Post: Any? and All? for MySQL
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
SELECT COUNT(*) AS total_students,
MAX(student_id = 130220) AS signed_up
FROM class_signups WHERE class_id = 1347
Can you tell what this does? The MAX aggregate function coupled with the comparison acts like Ruby’s Enumerable#any? Great if you’re retrieving a count and simultaneously want to ask, “Is the current user in that count?”
Similarly, the MIN aggregate function can be leveraged for Enumerable#all?.
SELECT COUNT(*) AS total_students,
MIN(status = 'ACTIVE') AS all_active
FROM class_signups WHERE class_id = 1347
Not having poked in the MySQL function book for a little while, maybe there are other ways to do this?