0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

MySQLのselectの基本操作

Posted at

あるテーブルの全ての列を出力する

-- *で全ての列を指定することができる
select * from Shohin;

あるテーブルの特定の列を出力する

-- select句の後に対象の列名を記述する
-- また、複数ある場合はカンマで区切る
select shohin_id, shohin_mei, shiire_tanka from Shohin;

別名をつける

ASを使用する。
日本語をつけることもできるが、その場合ダブルクォーテーションで囲む必要がある。

select shohin_id as ID, shohin_mei as "商品名", shiire_tanka as "仕入単価" from Shohin;

重複を除く

distinctを使用する。
nullについても、1つの種類として扱われる。

select distinct shohin_bunrui from Shohin;

複数列の前にdistinctを記述した場合、
複数行の組み合わせが全く同じ行が1つにまとめられる。

-- 以下の場合、shohin_bunrui、torokubiの組み合わせが同じもののみが1つにまとめられる。
-- 例えばshohin_bunruiの値が同じでも、torokubiの値が同じでなければ1つにまとめられない。
-- distinctは先頭の前にしか書くことができない。
-- そのため、distinct shohin_bunrui, distinct torokubiとは書けない。
select distinct shohin_bunrui, torokubi from Shohin;

条件の指定

whereを使用することで検索条件を指定することができる。

select * from Shohin where shohin_bunrui = "衣服";
```


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?