The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Any? and All? for MySQL

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Red Handed

Posts: 1158
Nickname: redhanded
Registered: Dec, 2004

Red Handed is a Ruby-focused group blog.
Any? and All? for MySQL Posted: Apr 29, 2005 12:58 PM
Reply to this message Reply

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
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Red Handed
Latest Posts From RedHanded

Advertisement

Seisyll Wyn sends in a tidy MySQL hack:

 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?

Read: Any? and All? for MySQL

Topic: Ruby in Hyderabad Previous Topic   Next Topic Topic: Rails job in Columbus, Ohio

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use