MySQL文法

更新日2009-06-09 (火) 08:48:10

select in

mysql> select * from ism_shouhin where ID in (10,12,15);
+------+---------------+------+--------+------+----------+------+
| ID   | name          | grp  | kakaku | url  | status   | etc  |
+------+---------------+------+--------+------+----------+------+
|   10 | ビール 500ml       | 201  |    360 |      |          |      |
|   12 | 発泡酒 500ml      | 201  |    164 |      |          |      |
|   15 | ワイン (ロゼ)        | 201  |    680 |      | 販売終了      |      |
+------+---------------+------+--------+------+----------+------+
3 rows in set (0.00 sec)
mysql> select * from ism_shouhin where ID not in (10,12,15);
+------+-------------------+------+--------+-----------------+------
| ID   | name              | grp  | kakaku | url | status    | etc  |
+------+-------------------+------+--------+-----------------+------
|    9 | ビール 350ml      | 201  |    250 |     |           |      |
|   11 | 発泡酒 350ml      | 201  |    134 |     |           |      |
|   13 | 焼酎 (芋)         | 201  |   1048 |     |           |      |
|   14 | 焼酎 (麦)         | 201  |   1148 |     |           |      |
|    16 | ワイン (白)       | 201  |    650 |     |           |      |
+------+-------------------+------+--------+-----+-----------+-------

テーブル名の一時変更

mysql> select ID,name as 名前,kakaku as 単価 from ism_shouhin where ID in(10,12,15);
+------+---------------+------+
| ID   | 名前             | 単価    |
+------+---------------+------+
|   10 | ビール 500ml       |  360 |
|   12 | 発泡酒 500ml      |  164 |
|    15 | ワイン (ロゼ)        |  680 |
+------+---------------+------+
3 rows in set (0.00 sec)

テーブル結合

select ism_uriage.* , ism_shouhin.* from ism_uriage join ism_shouhin on  
ism_uriage.shouhinid=ism_shouhin.ID where ism_uriage.uid=2 order  by
ism_uriage.ID desc limit 3;
+-----+---------------------------+------+-----------+-----------+------+------+
----------------------------+------+--------+-----------------------------------
--------------------------------------------+--------+------+
| ID  | date                      | uid  | shouhinid | uriagesuu | etc  | ID    |
 name                       | grp  | kakaku | url    | status | etc  |
+-----+---------------------------+------+-----------+-----------+------+------+
----------------------------+------+--------+-----------------------------------
--------------------------------------------+--------+------+
| 160 | 2008/10/02 (Thu) 15:52:20 | 2    |         1 |         1 | NULL |    1 |
 冷蔵庫 AB-12345(H)        | 101  |  45000 |        |        |      |
| 159 | 2008/10/02 (Thu) 15:52:20 | 2    |        18 |         4 | NULL |   18 |
 カクテルバー(レモン)       | 201  |    121 |        |        |      |
| 158 | 2008/10/02 (Thu) 15:52:20 | 2    |        21 |         2 | NULL |   21 |
 コンパクトカメラ(Leica C2) | 301  |  37590 |        |        |      |
+-----+---------------------------+------+-----------+-----------+------+------+
----------------------------+------+--------+-----------------------------------
--------------------------------------------+--------+------+

3 rows in set (0.01 sec)

テーブル作成・削除時のテーブル存在の確認

mysql> DROP TABLE IF EXISTS ism_test;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> create table if not exists ism_test( ID int8, date  varchar(64), etc  varchar(64));
Query OK, 0 rows affected (0.02 sec)

フィールド名および型の変更

型の変更(data)

mysql> show fields from ism_test;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ID    | bigint(20)  | YES  |     | NULL    |       |
| date  | varchar(64) | YES  |     | NULL    |       |
| etc   | varchar(64) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> alter table ism_test change date date int8;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> show fields from ism_test;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ID    | bigint(20)  | YES  |     | NULL    |       |
| date  | bigint(20)  | YES  |     | NULL    |       |
| etc   | varchar(64) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

名前の変更(data)

mysql> alter table ism_test change date data varchar(64);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> show fields from ism_test;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ID    | bigint(20)  | YES  |     | NULL    |       |
| data  | varchar(64) | YES  |     | NULL    |       |
| etc   | varchar(64) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

書式形式

alter table <テーブル名> change <フィールド名> <新フィールド名> <型> 

入力されるデータの範囲越えのとき

MySQLでは、定義さている値より多い値を入れようとすると、許容範囲まで入る。

dataが4Byte

mysql> show fields from ism_test;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ID    | int(11)     | YES  |     | NULL    |       |
| data  | varchar(4)  | YES  |     | NULL    |       |
| etc   | varchar(64) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

dataに5byte入力してみると4Byteまで入る

mysql> insert into ism_test values(1,'12345','岡田');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select * from ism_test;
+------+------+------+
| ID   | data | etc  |
+------+------+------+
|    1 | 1234 | 岡田     |
+------+------+------+
1 row in set (0.00 sec)
mysql> select * from ism_test;
+------+------+------+
| ID   | data | etc  |
+------+------+------+
|    1 | 1234 | 岡田     |
+------+------+------+
1 row in set (0.00 sec)

フィールドの型を変更すると矛盾すると値がなくなる (varchar=>int型に変更した場合)

mysql> alter table ism_test change etc etc int4;
Query OK, 1 row affected, 1 warning (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 1
mysql> select * from ism_test;
+------+------+------+
| ID   | data | etc  |
+------+------+------+
|    1 | 1234 |    0 |
+------+------+------+
1 row in set (0.00 sec)

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS