Skip to content

Instantly share code, notes, and snippets.

@psappho
psappho / rating.sql
Created November 6, 2011 13:45
日付が直近のレコードと比較してレーティングを求める
CREATE TABLE blog_entry (
written_by VARCHAR(32) NOT NULL,
entry_date DATE NOT NULL,
favs INT NOT NULL
)
;
ALTER TABLE blog_entry ADD CONSTRAINT UNIQUE (written_by, entry_date)
;
INSERT INTO blog_entry VALUES
@psappho
psappho / ranking.sql
Created November 4, 2011 01:39
ランキングを求める
/* **************************
ランキングを求める : MySQL
************************** */
create table products (
name varchar(32) not null,
price int not null
)
;
insert into products values
@psappho
psappho / temporary_lovers.sql
Created November 4, 2011 00:34
MySQLのTemporary Table(一時テーブル)について
create temporary table lovers
select * from (
select 'shidou' school, 'ギイ' pair1, '託生' pair2, TRUE is_coupled
union all select 'shidou' school, '三洲' pair1, '真行寺' pair2, TRUE is_coupled
union all select NULL school, '赤池' pair1, NULL pair2, FALSE is_coupled
) X
;
select * from lovers
;
@psappho
psappho / else_null.sql
Created November 4, 2011 00:29
SQL:CASE式のメモ
create table emp (
id int primary key auto_increment,
name varchar(32) not null unique, salary int not null
)
;
insert into emp (name, salary) values ('foo', 250000), ('bar', 320000), ('baz', 130000);
select
id, name, salary old_salary,
case