Home  >  Article  >  Database  >  MYSQL SET类型字段的SQL操作知识介绍_MySQL

MYSQL SET类型字段的SQL操作知识介绍_MySQL

WBOY
WBOYOriginal
2016-06-01 13:25:13872browse

bitsCN.com
`mark` set('index','best','hot') 

用的人少,懂的人也少,找了很久,总算凑齐了一套知识点
看上面的结构,MYSQL不傻,会存index吗,不会,存的是数字
SET字段是利用二进制数字来一一对应你设置的值,比如index,排在第0位,那么2的零次方等于1

update from table SET mark=1 


update from table SET mark='index' 

上面两个效果是一样的。
接着问题就来了,就像织梦那样,如果我想将一篇文章添加一个热门,但是不想删除其它标识在呢么办,或者我就想删除一个标识,我也不知道以前有什么标识。

以下是添加标识

update from table SET mark=mark |1|2

不要问我为什么用|这个东东,上面的意思是添加index、best两个标识,如果只添加hot标识,写成|4就可以了

下面是删除标识

update from table SET mark=mark &~4&~1 

好了,我先说了更新,下面说查询

SELECT * FROM table WHERE FIND_IN_SET('hot',mark) 

这个是最简单的查询方法,也可以用hot所在位置的二进制数4来替代查询,效率是一样的
也可以这么写:

SELECT * FROM table WHERE mark & 1 

bitsCN.com

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn