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⑥ レコードの更新、文字列の扱い

Posted at

レコードの更新、削除

全てのレコードのカラムを更新する update テーブル名 set 更新するカラム名 = 更新する値;

update users set score = 5.9;

特定のレコードだけ更新する update テーブル名 set 更新するカラム名 = 更新する値 where 条件にするカラム名 = 条件にする値;

update users set score = 5.9 where id = 1;

複数のカラムを更新する

update users set name = 'sasaki', score = 2.9 where name = 'tanaka';

レコードの全件削除

delete from users;

条件を指定して削除

delete from users where score < 5.0;

数値の演算

+ - * / %

カラムの値を計算して更新する

id が偶数の人のスコアを 1.2倍する。
update users set score = score * 1.2 where id % 2 = 0;

数値の丸め(四捨五入)

select round(5.355);  -> 5

小数点 1 桁目で丸める

select round(5.355, 1);  -> 5.4

小数点以下の切り捨て

select floor(5.833);  -> 5

小数点以下の切り上げ

select ceil(5.238);  -> 6

ランダムで抽出する

select rand();
*rand() を使うと 0 以上 1 未満のランダムな数値を返す。

rand()で抽選1名

select * from users order by rand() limit 1;

文字列の演算

文字数の表示

select length('Hello');  --> 5

何文字目以降を表示

select substr('Hello', 2); -->ello
select substr('Hello', 2, 3); -->ell

大文字にする

select upper('Hello'); --> HELLO

小文字にする

select lower('HELLO'); -->hello

連結する

select concat('hello', 'world'); --> helloworld

文字数でならべかえる

elect length(name), name from users order by length(name);

カラムに別名をつける as

select length(name) as len, name from users order by len;

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?