[前][次][番号順一覧][スレッド一覧]

mysql:7928

From: Koji KUSANAGI <Koji KUSANAGI <kusanagi@xxxxxxxxxx>>
Date: Fri, 20 Jun 2003 17:26:48 +0900
Subject: [mysql 07928] Re: SQL 文での WHERE 句の質問

草薙です。

>レコードの中身が複数、つまり
>dress_type = '5,7'
>と言うことです。

そのような設計の場合、神野様が [mysql 07923] で仰っているとおり、WHERE 
IN (1, 3, 5) で抽出されないことは自明であるかと思います。

個人的にはdress_type を int 型として、下記のようなテーブル連結を行った方
が、後々の扱いがスマートになるような気がします。

mysql> create table tbdress (num int, dress_name varchar(10));
Query OK, 0 rows affected (0.00 sec)

mysql> create table tbdress_a ( num int, dress_type int );
Query OK, 0 rows affected (0.00 sec)

mysql> insert into tbdress values(1, 'dress A');
Query OK, 1 row affected (0.00 sec)

mysql> insert into tbdress values(2, 'dress B');
Query OK, 1 row affected (0.00 sec)

mysql> insert into tbdress_a values(1, 5 );
Query OK, 1 row affected (0.00 sec)

mysql> insert into tbdress_a values( 1, 7 );
Query OK, 1 row affected (0.00 sec)

mysql> insert into tbdress_a values( 2, 3 );
Query OK, 1 row affected (0.00 sec)

mysql> insert into tbdress_a values( 2, 4 );
Query OK, 1 row affected (0.00 sec)

mysql> select * from tbdress;
+------+------------+
| num  | dress_name |
+------+------------+
|    1 | dress A    |
|    2 | dress B    |
+------+------------+
2 rows in set (0.00 sec)

mysql> select * from tbdress_a;
+------+------------+
| num  | dress_type |
+------+------------+
|    1 |          5 |
|    1 |          7 |
|    2 |          3 |
|    2 |          4 |
+------+------------+
4 rows in set (0.00 sec)

mysql> select t1.num, t1.dress_name, t2.dress_type from tbdress t1 inner join tb
dress_a t2 on t1.num=t2.num;
+------+------------+------------+
| num  | dress_name | dress_type |
+------+------------+------------+
|    1 | dress A    |          5 |
|    1 | dress A    |          7 |
|    2 | dress B    |          3 |
|    2 | dress B    |          4 |
+------+------------+------------+
4 rows in set (0.00 sec)

mysql> select t1.num, t1.dress_name, t2.dress_type from tbdress t1 inner join tb
dress_a t2 on t1.num=t2.num where t2.dress_type in ( 1,3,5 );
+------+------------+------------+
| num  | dress_name | dress_type |
+------+------------+------------+
|    1 | dress A    |          5 |
|    2 | dress B    |          3 |
+------+------------+------------+
2 rows in set (0.00 sec)



----------------------------------------------------
草薙考司 <kusanagi@xxxxxxxxxx>

[前][次][番号順一覧][スレッド一覧]

      7921 2003-06-20 16:33 ["K-A" <k-anzai@xxxxx] SQL 文での WHERE 句の質問               
      7922 2003-06-20 16:55 ┣[Koji KUSANAGI <kusan]                                       
      7924 2003-06-20 17:01 ┃┗["K-A" <k-anzai@xxxxx]                                     
      7925 2003-06-20 17:05 ┃ ┣[Kengo Jinno <kengo@x]                                   
->    7928 2003-06-20 17:26 ┃ ┗[Koji KUSANAGI <kusan]                                   
      7929 2003-06-20 18:20 ┃  ┗["K-A" <k-anzai@xxxxx]                                 
      7923 2003-06-20 16:56 ┣[Kengo Jinno <kengo@x]                                       
      7927 2003-06-20 17:12 ┃┗["K-A" <k-anzai@xxxxx]                                     
      7932 2003-06-21 16:06 ┃ ┗[seiji takegata <take]                                   
      7934 2003-06-22 16:40 ┃  ┗[SUGAWARA Hajime <sug]                                 
      7935 2003-06-22 20:49 ┃   ┗[seiji takegata <take]                               
      7926 2003-06-20 17:06 ┗[SUGAWARA Hajime <sug]