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?

MariaDB 基本構文 備忘録02

Posted at

MariaDB 基本構文 備忘録01「ここからアクセス

DISTINCT - 重複行の排除

"DISTINCT" なしの時
MariaDB [db]> SELECT 入金額 FROM 家計簿;
+-----------+
| 入金額    |
+-----------+
|         0 |
|    266000 |
|         0 |
|         0 |
|         0 |
|         0 |
|         0 |
+-----------+
7 rows in set (0.001 sec)

"DISTINCT" ありの時
MariaDB [db]> SELECT DISTINCT 入金額 FROM 家計簿;
+-----------+
| 入金額    |
+-----------+
|         0 |
|    266000 |
+-----------+
2 rows in set (0.001 sec)

ORDER BY - 結果の並び替え

"ORDER BY" なしの時
MariaDB [db]> SELECT * FROM 家計簿;
+------------+-----------------+-----------------------+-----------+-----------+
| 日付       | 費目            | メモ                  | 入金額    | 出金額    |
+------------+-----------------+-----------------------+-----------+-----------+
| 2024-02-03 | 食費            | コーヒーの購入        |         0 |       380 |
| 2024-02-10 | 給料            | 給料                  |    266000 |         0 |
| 2024-02-11 | 教育娯楽費      | 書籍代                |         0 |      2800 |
| 2024-02-14 | 交際費          | 会費                  |         0 |      5000 |
| 2024-02-18 | 水道光熱費      | 水道料金              |         0 |      5000 |
| 2024-02-20 | 通信費          | スマホ代              |         0 |      9000 |
| 2024-02-21 | 固定費          | 家賃                  |         0 |     12000 |
+------------+-----------------+-----------------------+-----------+-----------+
7 rows in set (0.000 sec)

"ORDER BY" ありの時
MariaDB [db]> SELECT * FROM 家計簿 ORDER BY 出金額;
+------------+-----------------+-----------------------+-----------+-----------+
| 日付       | 費目            | メモ                  | 入金額    | 出金額    |
+------------+-----------------+-----------------------+-----------+-----------+
| 2024-02-10 | 給料            | 給料                  |    266000 |         0 |
| 2024-02-03 | 食費            | コーヒーの購入        |         0 |       380 |
| 2024-02-11 | 教育娯楽費      | 書籍代                |         0 |      2800 |
| 2024-02-14 | 交際費          | 会費                  |         0 |      5000 |
| 2024-02-18 | 水道光熱費      | 水道料金              |         0 |      5000 |
| 2024-02-20 | 通信費          | スマホ代              |         0 |      9000 |
| 2024-02-21 | 固定費          | 家賃                  |         0 |     12000 |
+------------+-----------------+-----------------------+-----------+-----------+
7 rows in set (0.001 sec)

LIMIT句 - 上位 X 件を表示させたい

"LIMIT なしの時"
MariaDB [db]> SELECT 費目, 出金額  FROM 家計簿  ORDER BY 出金額 DESC;
+-----------------+-----------+
| 費目            | 出金額    |
+-----------------+-----------+
| 固定費          |     12000 |
| 通信費          |      9000 |
| 交際費          |      5000 |
| 水道光熱費      |      5000 |
| 教育娯楽費      |      2800 |
| 食費            |       380 |
| 給料            |         0 |
+-----------------+-----------+
7 rows in set (0.001 sec)

"LIMIT ありの時" "OFFSET 0"
MariaDB [db]> SELECT 費目, 出金額  FROM 家計簿  ORDER BY 出金額 DESC  LIMIT 3 OFFSET 0;
+-----------------+-----------+
| 費目            | 出金額    |
+-----------------+-----------+
| 固定費          |     12000 |
| 通信費          |      9000 |
| 水道光熱費      |      5000 |
+-----------------+-----------+
3 rows in set (0.001 sec)

"OFFSET":結果行の0行目から表示させる
"OFFSET 1 の時"
MariaDB [db]> SELECT 費目, 出金額  FROM 家計簿  ORDER BY 出金額 DESC  LIMIT 3 OFFSET 1;
+-----------------+-----------+
| 費目            | 出金額    |
+-----------------+-----------+
| 通信費          |      9000 |
| 交際費          |      5000 |
| 水道光熱費      |      5000 |
+-----------------+-----------+
3 rows in set (0.001 sec)
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?