Skip to content

Instantly share code, notes, and snippets.

@st98
Last active August 29, 2015 14:14
Show Gist options
  • Save st98/e58456ecce8eea18f849 to your computer and use it in GitHub Desktop.
Save st98/e58456ecce8eea18f849 to your computer and use it in GitHub Desktop.
create table s (x int, y int);
insert into s values (2, 2), (1, 3), (1, 1), (2, 1), (3, 1);
-- 普通の使い方
select y from s; -- 2 3 1 1 1
select * from s; -- 2|2 1|3 1|1 2|1 3|1
select y from s order by y; -- 1 1 1 2 3
select * from s order by x; -- 1|3 1|1 2|2 2|1 3|1
select * from s order by y; -- 1|1 2|1 3|1 2|2 1|3
-- 複数ソート、x でソートした後 y でソート
select * from s order by x, y; -- 1|1 1|3 2|1 2|2 3|1
-- select y from s order by y は select y from s order by 1 と等価
-- select * from s order by y は select * from s order by 2 と等価
-- select * from s order by x, y は order by 1, 2 と等価
select y from s order by 1; -- 1 1 1 2 3
select * from s order by 2; -- 1|1 2|1 3|1 2|2 1|3
select * from s order by 1, 2; -- 1|1 1|3 2|1 2|2 3|1
-- カラムの数が分かる
select * from s order by 2; -- 1|1 2|1 3|1 2|2 1|3
select * from s order by 3; -- Error: 1st ORDER BY term out of range - should be between 1 and 2
create table t (x int);
insert into t values (1), (2), (3);
select group_concat(x, ' ') from t order by 1, NULL; -- 1 2 3
select group_concat(x, ' ') from t order by 1, 0; -- Error: 2nd ORDER BY term out of range - should be between 1 and 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment