Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7509123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:48:51+00:00 2026-05-29T22:48:51+00:00

I am using the ruby mysql2 gem to work with a database. I want

  • 0

I am using the ruby mysql2 gem to work with a database. I want to list all the countries per region in one table, then add up the hits per region.

Normally I would use the mysql SUM function, but the gem returns the headers so this is not possible.

Instead, I am getting the hit count for each country per region and add it up.
The gem returns 1 array per result and I need to get that result and add it to a running total per region.

This is my code:

#!/usr/bin/ruby -w
# simple.rb - simple MySQL script using Ruby MySQL module

require "rubygems"
require "mysql2"

client = Mysql2::Client.new(:host => "localhost", :username => "root", :database => "cbscdn")
regions = client.query("select Id from Regions")

regions.each(:as => :array) do |rid|
  hit = client.query("select hits from Countries where Regions_Id='#{rid}'")
  hit.each(:as => :array) do |a|
    a.map do |x|
      x.to_i
    end
  end
end

How can I implement the running count per region?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:48:53+00:00Added an answer on May 29, 2026 at 10:48 pm

    If for any reason you don’t want to delegate it to the database engine, you can do this way:
    (for simplicity, I assume your results are already arrays)

    regions_count = regions.inject({}) do |hash, rid|
      hit = client.query("whatever")
      hash[rid] = hit.map(&:to_i).inject(0, :+)
      hash
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Rails 3.0.3 with REE ( Ruby 1.8.7 ) and gem 'mysql2', '0.2.6'
I have table with float column (I'm using MySQL, mysql2 gem, everything standard) create_table
I am using ruby on rails with a MySQL backend. I have a table
I am using Ruby on Rails. I want to create a filter field on
Currently I am using Ruby 1.9.1 and the 'ruby-mysql' gem, which unlike the 'mysql'
I'm attempting to install the 'mysql2' gem on my production server using Capistrano. I
After updating MySQL to 5.5 using apt-get, the mysql2 gem stopped working. Here's the
Using Ruby I'm trying to split the following text with a Regex ~foo\~\=bar =cheese~monkey
using Ruby on Rails 2.3.2, since I already created Scaffold for Story, so instead
Using Ruby (newb) and Regex, I'm trying to parse the street number from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.