您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“mysql的單列多值存儲(chǔ)實(shí)例分析”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
建表及數(shù)據(jù)準(zhǔn)備
-- 這里定義了bit(3),表示有3位,第一位1,第二位2,第三位4 create table t_bit_demo( id int NOT NULL AUTO_INCREMENT PRIMARY KEY, multi_value bit(3) not null default 0 ); -- 這里插入了1,2,4的組合值 insert into t_bit_demo(multi_value) values(b'000'); insert into t_bit_demo(multi_value) values(b'001'); insert into t_bit_demo(multi_value) values(b'010'); insert into t_bit_demo(multi_value) values(b'011'); insert into t_bit_demo(multi_value) values(b'100'); insert into t_bit_demo(multi_value) values(b'101'); insert into t_bit_demo(multi_value) values(b'110'); insert into t_bit_demo(multi_value) values(b'111'); -- 這里直接插入int值也可以,比如5相當(dāng)于101 -- insert into t_bit_demo(multi_value) values(5); SELECT multi_value+0, BIN(multi_value) FROM t_bit_demo; +---------------+------------------+ | multi_value+0 | BIN(multi_value) | +---------------+------------------+ | 0 | 0 | | 1 | 1 | | 2 | 10 | | 3 | 11 | | 4 | 100 | | 5 | 101 | | 6 | 110 | | 7 | 111 | +---------------+------------------+
位運(yùn)算查詢
-- 查詢第二位有值的數(shù)據(jù) select multi_value+0,BIN(multi_value) from t_bit_demo where multi_value & 2 +---------------+------------------+ | multi_value+0 | BIN(multi_value) | +---------------+------------------+ | 2 | 10 | | 3 | 11 | | 6 | 110 | | 7 | 111 | +---------------+------------------+ -- 查詢第三位有值的數(shù)據(jù) select multi_value+0,BIN(multi_value) from t_bit_demo where multi_value & 4 +---------------+------------------+ | multi_value+0 | BIN(multi_value) | +---------------+------------------+ | 4 | 100 | | 5 | 101 | | 6 | 110 | | 7 | 111 | +---------------+------------------+ -- 查詢只有第三位有值的數(shù)據(jù) select multi_value+0,BIN(multi_value) from t_bit_demo where multi_value = 4 select multi_value+0,BIN(multi_value) from t_bit_demo where multi_value = 4 +---------------+------------------+ | multi_value+0 | BIN(multi_value) | +---------------+------------------+ | 4 | 100 | +---------------+------------------+
更新
select id,multi_value+0,BIN(multi_value) from t_bit_demo +----+---------------+------------------+ | id | multi_value+0 | BIN(multi_value) | +----+---------------+------------------+ | 1 | 0 | 0 | | 2 | 1 | 1 | | 3 | 2 | 10 | | 4 | 3 | 11 | | 5 | 4 | 100 | | 6 | 5 | 101 | | 7 | 6 | 110 | | 8 | 7 | 111 | +----+---------------+------------------+ -- 將id為7的值移除第二個(gè)枚舉 update t_bit_demo set multi_value = b'100' where id=7 select id,multi_value+0,BIN(multi_value) from t_bit_demo where id=7 +----+---------------+------------------+ | id | multi_value+0 | BIN(multi_value) | +----+---------------+------------------+ | 7 | 4 | 100 | +----+---------------+------------------+
建表及數(shù)據(jù)準(zhǔn)備
create table t_bigint_demo( id int NOT NULL AUTO_INCREMENT PRIMARY KEY, multi_value bigint not null default 0 ); -- 假設(shè)這里定義了1,2,4三個(gè)枚舉值 insert into t_bigint_demo(multi_value) values(0); insert into t_bigint_demo(multi_value) values(1); insert into t_bigint_demo(multi_value) values(2); insert into t_bigint_demo(multi_value) values(3); insert into t_bigint_demo(multi_value) values(4); insert into t_bigint_demo(multi_value) values(5); insert into t_bigint_demo(multi_value) values(6); insert into t_bigint_demo(multi_value) values(7); select multi_value from t_bigint_demo +-------------+ | multi_value | +-------------+ | 0 | | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | +-------------+
查詢
-- 查詢包含第二個(gè)枚舉的數(shù)據(jù) select multi_value,BIN(multi_value) from t_bigint_demo where multi_value & 2 +-------------+------------------+ | multi_value | BIN(multi_value) | +-------------+------------------+ | 2 | 10 | | 3 | 11 | | 6 | 110 | | 7 | 111 | +-------------+------------------+ -- 查詢包含第三個(gè)枚舉的數(shù)據(jù) select multi_value,BIN(multi_value) from t_bigint_demo where multi_value & 4 +-------------+------------------+ | multi_value | BIN(multi_value) | +-------------+------------------+ | 4 | 100 | | 5 | 101 | | 6 | 110 | | 7 | 111 | +-------------+------------------+ -- 查詢值為第三個(gè)枚舉的數(shù)據(jù) select multi_value,BIN(multi_value) from t_bigint_demo where multi_value =4 +-------------+------------------+ | multi_value | BIN(multi_value) | +-------------+------------------+ | 4 | 100 | +-------------+------------------+
更新
select id,multi_value,BIN(multi_value) from t_bigint_demo +----+-------------+------------------+ | id | multi_value | BIN(multi_value) | +----+-------------+------------------+ | 1 | 0 | 0 | | 2 | 1 | 1 | | 3 | 2 | 10 | | 4 | 3 | 11 | | 5 | 4 | 100 | | 6 | 5 | 101 | | 7 | 6 | 110 | | 8 | 7 | 111 | +----+-------------+------------------+ -- 將id為7的值移除第二個(gè)枚舉 update t_bigint_demo set multi_value = b'100' where id=7 select id,multi_value,BIN(multi_value) from t_bigint_demo where id=7 +----+-------------+------------------+ | id | multi_value | BIN(multi_value) | +----+-------------+------------------+ | 7 | 4 | 100 | +----+-------------+------------------+
建表及數(shù)據(jù)準(zhǔn)備
create table t_varchar_demo( id int NOT NULL AUTO_INCREMENT PRIMARY KEY, multi_value varchar(255) not null default '' ); -- 假設(shè)這里定義了1,2,4三個(gè)枚舉值 insert into t_varchar_demo(multi_value) values('1'); insert into t_varchar_demo(multi_value) values('2'); insert into t_varchar_demo(multi_value) values('1,2'); insert into t_varchar_demo(multi_value) values('4'); insert into t_varchar_demo(multi_value) values('1,4'); insert into t_varchar_demo(multi_value) values('2,4'); insert into t_varchar_demo(multi_value) values('1,2,4'); select multi_value from t_varchar_demo +-------------+ | multi_value | +-------------+ | 1 | | 2 | | 1,2 | | 4 | | 1,4 | | 2,4 | | 1,2,4 | +-------------+
查詢
-- 查詢包含第二個(gè)枚舉的數(shù)據(jù) select multi_value from t_varchar_demo where find_in_set('2',multi_value) +-------------+ | multi_value | +-------------+ | 2 | | 1,2 | | 2,4 | | 1,2,4 | +-------------+ -- 查詢包含第三個(gè)枚舉的數(shù)據(jù) select multi_value from t_varchar_demo where find_in_set('4',multi_value) +-------------+ | multi_value | +-------------+ | 4 | | 1,4 | | 2,4 | | 1,2,4 | +-------------+ -- 查詢只有第三個(gè)枚舉的數(shù)據(jù) select multi_value from t_varchar_demo where multi_value = '4' +-------------+ | multi_value | +-------------+ | 4 | +-------------+
更新
select * from t_varchar_demo +----+-------------+ | id | multi_value | +----+-------------+ | 1 | 1 | | 2 | 2 | | 3 | 1,2 | | 4 | 4 | | 5 | 1,4 | | 6 | 2,4 | | 7 | 1,2,4 | +----+-------------+ -- 將id為7的值移除第二個(gè)枚舉 update t_varchar_demo set multi_value = '1,4' where id=7 select * from t_varchar_demo where id=7 +----+-------------+ | id | multi_value | +----+-------------+ | 7 | 1,4 | +----+-------------+
建表及數(shù)據(jù)準(zhǔn)備
create table t_set_demo( id int NOT NULL AUTO_INCREMENT PRIMARY KEY, multi_value set('1','2','4') not null default '' ); insert into t_set_demo(multi_value) values(''); insert into t_set_demo(multi_value) values('1'); insert into t_set_demo(multi_value) values('2'); insert into t_set_demo(multi_value) values('1,2'); insert into t_set_demo(multi_value) values('4'); insert into t_set_demo(multi_value) values('1,4'); insert into t_set_demo(multi_value) values('2,4'); insert into t_set_demo(multi_value) values('1,2,4');
查詢
-- 查詢包含第二個(gè)枚舉的數(shù)據(jù),可以用位運(yùn)算也可以用find_in_set select multi_value from t_set_demo where multi_value&2 select multi_value from t_set_demo where find_in_set('2',multi_value) +-------------+ | multi_value | +-------------+ | 2 | | 1,2 | | 2,4 | | 1,2,4 | +-------------+ -- 查詢包含第三個(gè)枚舉的數(shù)據(jù),可以用位運(yùn)算也可以用find_in_set select multi_value from t_set_demo where multi_value&4 select multi_value from t_set_demo where find_in_set('4',multi_value) +-------------+ | multi_value | +-------------+ | 4 | | 1,4 | | 2,4 | | 1,2,4 | +-------------+ -- 查詢值為第三個(gè)枚舉的數(shù)據(jù) select multi_value from t_set_demo where multi_value='4' +-------------+ | multi_value | +-------------+ | 4 | +-------------+
更新
select * from t_set_demo +----+-------------+ | id | multi_value | +----+-------------+ | 1 | | | 2 | 1 | | 3 | 2 | | 4 | 1,2 | | 5 | 4 | | 6 | 1,4 | | 7 | 2,4 | | 8 | 1,2,4 | +----+-------------+ -- 將id為7的值移除第二個(gè)枚舉 update t_set_demo set multi_value = '1,4' where id=7 select * from t_set_demo where id=7 select * from t_set_demo where id=7 +----+-------------+ | id | multi_value | +----+-------------+ | 7 | 1,4 | +----+-------------+
“mysql的單列多值存儲(chǔ)實(shí)例分析”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。