<?xml version="1.0" encoding="UTF-8"?>
<codes type="array">
  <code>
    <code>duplicates = User.find( :all,
  :select     =&gt; &quot;email, COUNT(email) AS duplicate_count&quot;,
  :conditions =&gt; &quot;email IS NOT NULL AND email != ''&quot;,
  :group      =&gt; &quot;email HAVING duplicate_count &gt; 1&quot;
)</code>
    <created-at type="datetime">2009-07-18T01:52:55Z</created-at>
    <description>having example in rails. from: http://blog.grayproductions.net/articles/five_activerecord_tips</description>
    <id type="integer">187</id>
    <language-id type="integer">39</language-id>
    <privated type="boolean">false</privated>
    <title>finding duplicates entries</title>
    <updated-at type="datetime">2009-07-18T01:52:55Z</updated-at>
    <user-id type="integer">7</user-id>
  </code>
  <code>
    <code>SELECT COUNT(*) AS Total, SUBSTRING_INDEX(email, '@', -1) AS Domain FROM users 
GROUP BY SUBSTRING_INDEX(email, '@', -1) ORDER BY COUNT(*) DESC LIMIT 10;</code>
    <created-at type="datetime">2009-07-12T17:47:15Z</created-at>
    <description>from: http://www.mendable.com/sql-trick-where-are-your-users-from/</description>
    <id type="integer">186</id>
    <language-id type="integer">40</language-id>
    <privated type="boolean">false</privated>
    <title>count domain users using sql</title>
    <updated-at type="datetime">2009-07-12T17:50:03Z</updated-at>
    <user-id type="integer">7</user-id>
  </code>
  <code>
    <code>-- mostra o n&#250;mero de usu&#225;rios novos criados dia a dia
select date_format(created_at,'%d/%m/%y') as data, count(id) as total from leonardo_supertestes.users group by date_format(created_at,'%m-%d') order by date_format(created_at,'%m-%d');

-- mostra o n&#250;mero de testes feitos dia a dia
select date_format(created_at,'%d/%m/%y') as data, count(rate) as total from leonardo_supertestes.results group by date_format(created_at,'%m-%d') order by date_format(created_at,'%m-%d');

-- conta o n&#250;mero total de usu&#225;rios
select count(id) from leonardo_supertestes.users;

-- conta o n&#250;mero total de testes feitos
select sum(hits) from leonardo_supertestes.exams;</code>
    <created-at type="datetime">2009-05-24T16:45:07Z</created-at>
    <description></description>
    <id type="integer">177</id>
    <language-id type="integer">40</language-id>
    <privated type="boolean">true</privated>
    <title>autosimulado queries</title>
    <updated-at type="datetime">2009-05-24T16:45:07Z</updated-at>
    <user-id type="integer">7</user-id>
  </code>
  <code>
    <code># first, create the method import_sql:

class ActiveRecord::ConnectionAdapters::MysqlAdapter
  def import_sql(file)
    conf = ActiveRecord::Base.configurations[RAILS_ENV]
    sql_file = File.join(RAILS_ROOT, 'db', file + '.sql')
    cmd_line = &quot;mysql -h &quot;+conf[&quot;host&quot;]+&quot; -D &quot;+conf[&quot;database&quot;]+ &quot; --user=&quot;+conf[&quot;username&quot;]+&quot; --password=&quot;+conf[&quot;password&quot;]+&quot; &lt; &quot;+sql_file
    raise Exception, &quot;Error executing &quot; + cmd_line unless system(cmd_line)    
  end
end


# create a migration. ex:

./script/generate migration sql_external_test

# save your sql file with sql commands in db/. migration's sample:

class SqlExternalTest &lt; ActiveRecord::Migration
  def self.up
    import_sql(&quot;test&quot;)
  end

  def self.down
  end
end

</code>
    <created-at type="datetime">2009-02-02T18:46:51Z</created-at>
    <description>it's a small how to import a file in migrations.

from: http://code-ronin.com/articles/rails-migrations-directly-import-sql</description>
    <id type="integer">151</id>
    <language-id type="integer">39</language-id>
    <privated type="boolean">false</privated>
    <title>import sql files into migrations</title>
    <updated-at type="datetime">2009-02-02T18:46:51Z</updated-at>
    <user-id type="integer">7</user-id>
  </code>
  <code>
    <code>ALTER TABLE tablename AUTO_INCREMENT = 1</code>
    <created-at type="datetime">2008-07-02T00:31:31Z</created-at>
    <description></description>
    <id type="integer">10</id>
    <language-id type="integer">40</language-id>
    <privated type="boolean">false</privated>
    <title>resets autoincrement (mysql)</title>
    <updated-at type="datetime">2008-07-02T00:31:31Z</updated-at>
    <user-id type="integer">7</user-id>
  </code>
</codes>
