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.

DISTINCT SQL 重複しているものは表示しない

Posted at

用途

SQLで重複しているものは表示させたくない時に使う。

DISTINCTの使用方法

書き方
SELECT DISTINCT 指定するカラム名, 指定するカラム名, ... FROM テーブル名 ;

使用するhogeテーブル

name color number
suzuki red 10
suzuki red 10
takahashi blue 10

DISTINCT無しの場合

DISTINCT無し
SELECT number from hoge;

結果

number
10
10
10

DISTINCTありの場合

DISTINCTあり
SELECT DISTINCT number from hoge;

結果

number
10

※numberカラムで重複しているものを削除して表示される。

DISTINCT無しでカラム指定が複数の場合

DISTINCT無しでカラム指定が複数
SELECT color, number from hoge;

結果

color number
red 10
red 10
blue 10

DISTINCTありでカラム指定が複数の場合

DISTINCT無しでカラム指定が複数
SELECT DISTINCT color, number from hoge;

結果

color number
red 10
blue 10

※ colorカラム と numberカラム の両方が重複しているものを削除して表示される。

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?