LoginSignup
3
0

More than 5 years have passed since last update.

第10回つぶやき勉強会 ~SQLの世界~

Posted at
1 / 6

MySQLでやりたいとおもいます。まずは、テーブルとデータを用意します。


create table users (
  id int,
  name text,
  level float comment '0~1の間の少数'
);

insert users values 
(1, 'taro', 0.2),
(2, 'yuki', 0.2),
(3, 'ken', 0.5),
(4, 'tera', null)
;

mysql> select * from users;
+------+------+-------+
| id   | name | level |
+------+------+-------+
|    1 | taro |  0.20 |
|    2 | yuki |  0.20 |
|    3 | ken  |  0.50 |
|    4 | tera |  NULL |
+------+------+-------+

次の2つのコマンドの結果は何でしょう?

select count(*) from users where level != null;
select count(*) from users where level != 0.2;

答え

0
3


解説

0.2 は float だと、2進数世界において無限小数となる。 decimal(x,y) を使うことで回避できたりします。

3
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
3
0