端くれプログラマの備忘録 MySQL [MySQL] 基本メモ: レコードの抽出

[MySQL] 基本メモ: レコードの抽出

> select * from users;
+------+--------+--------------------+----------+-------+------+-----------+---------------------+
| id   | name   | email              | password | score | sex  | memo      | created             |
+------+--------+--------------------+----------+-------+------+-----------+---------------------+
| NULL | yamada | yamada@example.com | password |    10 | NULL | test memo | 2015-05-01 01:23:45 |
+------+--------+--------------------+----------+-------+------+-----------+---------------------+
1 row in set (0.00 sec)

> select * from users G;
*************************** 1. row ***************************
      id: NULL
    name: yamada
   email: yamada@example.com
password: password
   score: 10
     sex: NULL
    memo: test memo
 created: 2015-05-01 01:23:45
1 row in set (0.00 sec)

ERROR:
No query specified

> select name,email from users;
> select * from users where <条件>
> select * from users order by score;
> select * from users order by score desc;
> select * from users limit 3; 件数
> select * from users limit 2, 2; オフセット, 件数

条件の例

  • value >= 10
  • value = 10
  • value != 10
  • value <> 10
  • value between 5.0 and 15.0 (5.0と15.0は含まれる)
  • color != “red”
  • color in(‘red’,’blue’)  (どれか1つにマッチ)
  • created > ‘2015-05-01 02:00:00’ (日時判定)
  • email like ‘%@google.com’
  • email like ‘%@google.___’
  • ‘%’は任意の0文字以上の文字列を示す。
  • ‘_’は任意の1文字を示す。
  • value > 5 and color = ‘white’ (and条件)
  • value > 5 or color = ‘white’ (or条件)