Equivalent scrapbook

…scrap on Database mysql

mysql select uniq (distinct, non-duplicate) items with SELECT DISTINCT

SELECT DISTINCT type


mysql -u root -p

inporting sql

mysql -u root -p rails_development < rails.db

general mysql commands

show databases;
show tables;
drop database database_name;
create database database_name;
use database_name;
desc table;

select count(*) from categories;

random record in mysql

Person.find(:first, :order => 'rand()')
Model.first(:order => "RANDOM()") 
Thing.order("RANDOM()").first
Thing.offset(rand(Thing.count)).first
Recommendation.offset(rand(Recommendation.count)).where('person_id != 1').first

trigger direct mysql command from rails

ActiveRecord::Base.connection.execute("TRUNCATE #{ApplicationBuildCommand.table_name}")

change password on mysql user

use mysql;
update user set password=PASSWORD("NEW-PASSWORD-HERE") where User='tom';