sql server instructions help 入門 sponsored link


selectの最近のブログ記事

★SQL★  コマンド DISTINCT 重複行 削除


通常SELECT文での問い合わせ結果には

重複行が含まれることがあります。

こうした検索内容から重複する行を取除くには?

答え:SELECT DISTINCTを使います。

DISTINCT キーワードは、

★SQL★ ★SQL★ 文に1回のみ使用可能です。

全てのフィールドを選択出力す場合、
レコードと全く同じレコード以外が出力されます。

例)select distinct * from table1

通常は全件出ることでしょう。
つまり select * from table1 と同じになります。
sql コマンド group by グループ化

group by は、
テーブル内で選択された列で同じ値の行をグループ化します。

select * from table_1 btoup by NO;

select * sum(mny) from fruit_table group by fruit;

sum()は、グループ全体について
値を計算します。

select b,sum(c) from table group by b having sum(c) > 20;

select b,sum(c) from table group by b having b < 'a';
★SQL★ コマンド データ 並び替えORDER BY

抽出したデータの並び替えを行うには、
ORDER BY を使用する。
★SQL★  のorder by を使用した並び替えは、
列ごとに

昇順、
降順、
NULL

の表示順を設定できる。

※昇順とは、データの日付を古いものから新しいものへ、
データ量の小さいものから大きいものへ、
または文字を50音順に、
または、アルファベットをAからZの方向へと
並び替えることである。
逆の並べ方は降順と呼ばれる。


並び替えキーワードは
昇順:ASC(無指定時 デフォルト) と
降順:DESC
がある。


例 order by とDESCを使った例

SELECT * FROM table  ORDER BY name DESC


★SQL★ の例 order by とascを使った例

SELECT * FROM table  ORDER BY name asc
例1.テーブルの別名
select * from table1 as t1;
select * from table1 t1;

asキーワードは省略できる。





tableからlistの行を取り出し
select list from table;


table から全ての行を取り出し
select * from table;

select cat,a + b from table;
a と b は数値型と仮定する。

select 100 * 200;
電卓として使用できる

select random();
random()という関数を呼び出す。

select * from table1,table2

select * from table1 cross join table2;
クロス結合

select * from table1 inner join table2 on true;

select * from table1 cross join table2;

select * from table1 inner join table2 on table1.number = table2.number;

select * from table1 inner join table2 using (number);
カラムnumber をキーにして、結合

select * from table1 natural inner join table2;
naturalはusingの略式です。

select * from table1 left join table2 on table1.number = table2.number;

select * from table1 left join table2 using (number);

select * from table1 right join table2 on table1.number = table2.number;

select * from table1 full join table2 on table1.number = table2.number;














Buzzurl/ hatena/ MyYahoo!/ livedoorClip/ Google/ niftyclip/ pookmark/ flog/ FC2/

category

SQL


<<select>>
banner_14.gif

<<SQL>>
banner_14.gif

<<sql distinct>>
banner_14.gif

ブログランキング