LoginSignup
4
4

More than 5 years have passed since last update.

MySQLまとめ その3

Posted at

値の余り

select mod(123, 10); 結果 3

四捨五入

select round(10.345, 1); 結果 10.3
select round(10.345, 2); 結果 10.35

切り捨て

select truncate(10.349, 2); 結果 10.34

切り上げ

select truncate(10.345 + .9, 0); 結果 11

平方根

select sqrt(2.0); 結果 1.4142135623730951

べき乗

select pow(2, 8); 結果 256

文字列結合

select concat(123456, 789012);

テーブル使用時

select concat(key1, data1) from testm;

char 文字変換

select char(49);
ascii コード変換
select ascii('1');

小文字化

select lower('ABC012');

大文字化

select upper('abc012');

左文字埋め

select lpad('abc', 5, '012');

右文字埋め

select rpad('abc', 5, '012');

サンプル

SELECT RPAD( 'MySQL', 10, '+' ); 結果MySQL+++++

左空白削除

select ltrim(' abc');

右空白削除

select ltrim('abc ');

左右空白削除

select trim(' abc ');

文字列置き換え

select replace('a0a1a2', 'a0', 'b1');

文字列切り出し

select substring('abc012', 3, 2);

文字列検索

select locate('c0', 'abc012');

文字列の長さ

select length('abc012');

文字列を繰り返す

select repeat('abc012', 2);

文字列数値変換

select concat(123456, 789012);

多重判定

select key1,
case
when key1 = 'a001' then 'aaa'
when key1 = 'b002' then 'bbb'
when key1 = 'c003' then 'ccc'
else 'xxx'
end c1
from testm
;

システム時間・システム日付

select current_date; 結果 2015-02-09

select current_time; 結果 09:18:46

select current_timestamp; 結果 2015-02-09 09:19:12

select now(); 結果 2015-02-09 09:20:39

select date_format(now(), '%y.%m.%d'); 結果 15.02.09

select date_format(now(), '%Y.%m.%d'); 結果 2015.02.09

日付切り捨て

select cast(substring(now(), 1, 10) as date); 結果 2015-02-09

日付の差

select to_days(now()) - to_days('2013-09-01 00:00:00'); 結果 526

日付の加算

select date_add(now(), interval 3 day); 結果 2015-02-12 10:27:07

テーブルのコピー

create table workw as
select
*
from testm
where key1 like 'a%'
;
alter table workw type=InnoDB;

コピーしたテーブルの表示

select * from workw;

URL

4
4
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
4
4