私はそれに対してお勧めします、それは正規化規則に反します。私は1NFを台無しにし続けるか、正規化タグ
の下の投稿を読んで
ください。
テーブルの再設計提案
このようにタグとタグリンクテーブルを作成すると。
table tag
id autoincrement integer primary index
tag_str varchar(20) index unique not null
table taglink
id autoincrement integer primary index #gotta have an ID in my book.
tag_id integer not null
product_id integer not null
このような販売テーブルがあります。
table product
id autoincement integer primary index
desc varchar(255)
barcode, price, whatever ...
タグごとに製品を検索する select ステートメント
次のようにタグに一致する記事を検索できます。
select * from product
inner join taglink on (product.id = taglink.product_id)
inner join tag on (taglink.tag_id = tag.id)
where tag.tag_str in ('foo','bar','baz');
製品ごとにタグを一覧表示する select ステートメント
select tag_str from tag
inner join taglink on (taglink.tag_id = tag.id)
inner join product on (taglink.product_id = product.id)
where product.barcode = '4548215' or product.desc like 'OMG Po%'
新しいタグの追加
新しいタグを追加するには、
insert into tag (id, tag_str) values (
null /*remember autoincrement*/
,'Mytag');
タグのリンク
タグを商品にリンクするには
set @product_id = 10;
set @tag_id = 1;
...or...
select @product_id:= product.id from product where product.barcode = '1254851';
...
insert into taglink (id, product_id, tag_id) values (
null /*autoinc id*/
,@product_id
,@tag_id );
FIND_IN_SET
無制限の数のタグを製品にリンクでき、コストのかかるステートメントでクエリを遅くすることはありません.
また、タグの重複を防ぎます。
また、データベースはより高速で小さくなります。