[from now] 2013/01/24 22:22:24

diff --git a/basic_sql.rb b/basic_sql.rb
new file mode 100644
index 0000000..b7537d3
--- /dev/null
+++ b/basic_sql.rb
@@ -0,0 +1,37 @@
+# -*- coding: utf-8 -*-
+# SQL
+
+require "active_record"
+require "pp"
+
+ActiveRecord::Base.logger = nil
+# ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new(STDOUT)
+ActiveSupport::LogSubscriber.colorize_logging = false
+ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
+silence_stream(STDOUT) do
+  ActiveRecord::Schema.define do
+    create_table :articles do |t|
+      t.string :name
+      t.integer :score
+      t.timestamps
+    end
+  end
+end
+
+class Article < ActiveRecord::Base
+end
+
+Article.create!(:name => "e", :score => 0)
+Article.create!(:name => "d", :score => 3)
+Article.create!(:name => "c", :score => 3)
+Article.create!(:name => "b", :score => 4)
+Article.create!(:name => "a", :score => 5)
+
+pp ActiveRecord::Base.connection.select_all("
+SELECT a.name, a.score, COUNT(a.name) AS rank
+FROM articles a, articles b
+WHERE (a.score < b.score or a.name = b.name)
+AND a.score >= 0
+GROUP BY a.name, a.score HAVING rank <= 100
+ORDER BY rank
+")
SQL汎用版の追加