搞不明白的数据库位运算

数据库字段 visible set('user','memb','agent','adm') NOT NULL default 'user',

条件 WHERE visible & 3 是什么意思

SELECT * FROM AgentsDesc WHERE visible & 3
帮我问下。

————————————————————————————————————
下面是表,供测试,我怎么测试都不对,晕,

CREATE TABLE AgentsDesc (
ID int(10) unsigned NOT NULL auto_increment,
name varchar(64) NOT NULL default '',
namedisp varchar(255) NOT NULL default '',
namenote varchar(255) NOT NULL default '',
extra mediumtext NOT NULL,
type enum('d','f','c','p','e','en','a','s','0','r','rb','set','eny','rr','js') NOT NULL default 'd',
order float NOT NULL default '0',
join_page float NOT NULL default '0',
profile_type varchar(10) NOT NULL default 'single',
visible set('user','memb','agent','adm') NOT NULL default 'user',
editable set('memb','agent','adm') NOT NULL default 'agent,adm',
check varchar(255) NOT NULL default '',
because varchar(255) NOT NULL default '',
group_mark enum('','b','c','e') NOT NULL default '',
group_text_b varchar(32) NOT NULL default '',
group_text_a varchar(32) NOT NULL default '',
to_db int(11) NOT NULL default '1',
show_on_page set('0','3','7','25') NOT NULL default '0',
get_value varchar(255) NOT NULL default '',
get_value_db varchar(255) NOT NULL default '',
search_type enum('none','radio','list','check','daterange','check_set','text','js') default 'none',
search_cols int(11) NOT NULL default '0',
search_hide tinyint(4) NOT NULL default '0',
search_extra varchar(255) NOT NULL default '',
search_order float NOT NULL default '0',
search_default varchar(255) NOT NULL default '',
search_where varchar(255) NOT NULL default '',
match_field varchar(64) NOT NULL default '',
match_type enum('none','enum','enum_ref','set','daterange') NOT NULL default 'none',
match_extra varchar(255) NOT NULL default '',
min_length varchar(5) NOT NULL default '',
max_length varchar(5) NOT NULL default '',
PRIMARY KEY (ID),
UNIQUE KEY name (name)
) type=MyISAM AUTO_INCREMENT=151 ;

--
-- 导出表中的数据 AgentsDesc
--

INSERT INTO AgentsDesc VALUES (3, 'Name', '_Name_caption', '_AGENT_LEAST', '40', 'c', 1, 1020, 'single', 'user,agent,adm', 'agent,adm', 'return (strlen($arg0) >= 2 && strlen($arg0) <= 20 ) ? true : false;', '_AGENT_LEAST', '', '', '', 1, '3,25', '', '', 'none', 0, 0, '', 0, '', '', '', 'none', '', '2', '20');
INSERT INTO AgentsDesc VALUES (4, 'Contact', '_Contact_caption', '_Use latin set', '40', 'c', 4, 1030, 'single', 'memb,adm', 'memb,adm', 'return strlen($arg0) > 0 ? true : false;', '_Contact_err_msg', '', '', '', 1, '0', '', '', 'none', 0, 0, '', 0, '', '', '', 'none', '', '', '');
INSERT INTO AgentsDesc VALUES (6, 'Country', '_Country', '_MUST_BE_VALID', '', 'js', 5.5, 2020, 'single', 'user,agent,adm', 'agent,adm', 'return strlen($arg0) > 0 ? true : false;', '_CITY_REQUIRED', '', '', '', 1, '3,25', '', '', 'none', 0, 0, '', 1, '-1', '', 'Country', 'none', '5', '', '');
INSERT INTO AgentsDesc VALUES (29, 'Email,Email1', '_E-mail', 'MUST_BE_VALID', '40', 'c', 29.5, 1001, 'single', 'agent,adm', 'agent,adm', 'return (eregi("^[.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,4}$", $arg0) && !strcmp($arg0,$_POST[Email2]) && conf_email($arg0,$_COOKIE[memberID])) ? true : false;', '_EMAIL_INVALID', '', '', '', 1, '3', '', '', 'none', 0, 0, '', 0, '', '', '', 'none', '', '5', '128');
INSERT INTO AgentsDesc VALUES (31, 'Email,Email2', '_Confirm E-mail', '_MUST_BE_VALID', '40', 'c', 32, 1003, 'single', 'agent,adm', 'agent,adm', '', '', '', '', '', 0, '3', '', '', 'none', 0, 0, '', 0, '', '', '', 'none', '', '', '');
INSERT INTO AgentsDesc VALUES (32, 'HomePage', '_Homepage', '_HomePage_desc', '40', 'c', 0, 2070, 'single', 'user,agent,adm', 'agent,adm', 'return strlen($arg0) > 0 ? true : false;', '_HomePage_err_msg', '', '', '', 1, '3', '', '', 'none', 0, 0, '', 0, '', '', '', 'none', '', '', '');
INSERT INTO `AgentsDe
---------------------------------------------------------------

200分,接分快乐

mysql> select 0&3,1&3,2&3,3&3,4&3,5&3,6&3,7&3,8&3,9&3;
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
¦ 0&3 ¦ 1&3 ¦ 2&3 ¦ 3&3 ¦ 4&3 ¦ 5&3 ¦ 6&3 ¦ 7&3 ¦ 8&3 ¦ 9&3 ¦
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
¦ 0 ¦ 1 ¦ 2 ¦ 3 ¦ 0 ¦ 1 ¦ 2 ¦ 3 ¦ 0 ¦ 1 ¦
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
和mod运算一样哈
---------------------------------------------------------------

是呀,怎么能跟3做位运算呢?

一个SET对象的大小由不同的集合成员的数量决定。如果集合大小是N,对象占据(N+7)/8个字节,四舍
五入为1,2,3,4或8 个字节。一个SET最多能有64个成员。

visible set('user','memb','agent','adm') NOT NULL default 'user',

值 对应二进制
'user' 00000001
'memb' 00000010
'agent' 00000100
'adm' 00001000

3 00000011

于是 WHERE visible & 3 将筛选出
visible 为'user'或'memb'的记录

一直都用长整型存储权限位,还真没注意set类型可以多出一倍

Published At
Categories with Web编程
comments powered by Disqus