数据库字段复用的基本原理与示例
一、逻辑代数基础:
1,数字用二进制表示,所有可能出现的数只有0和1两个。
2,基本运算只有“与”、“或”、“非”三种。
与运算定义为:(用 & 表示与运算)
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
可以简单理解为:只要有一个0,结果就是0,和乘法类似。
或运算定义为:(用 | 表示与运算)
0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1
可以简单理解为:只要有一个1,结果就是1,和加法类似。
二、逻辑运算示例:
01111010101010101111111111111111 & 1100000 = 1100000
一般可以理解为:
如果要获取一个数字某N位的数值,只需要将这个数字与2的N-1次方(掩码)进行与运算即可。
三、数据库字段定义:
以数据表 binary_sample为例:
create table binary_sample(
uid int unsigned not null,
status int unsigned not null default 0,
primary key(uid),
key i_s(status)
)engine=innodb;
status字段定义:
status字段数据类型为32bit的整数,为了尽可能的存储多个属性,我们将其进行如下定义:
以下所有“位”的描述顺序按照从低到高(从右到左)顺序表示。
0-2位表示用户注册状态:
000 表示新注册未被批准
001 表示注册被批准
010 表示位高级用户
011 表示管理员
100 表示超级管理员
101 保留
110 保留
111 掩码
3-5位用户性别:
000 表示性别不确定
001 表示性别为男
010 表示性别为女
011 保留
100 保留
101 保留
110 保留
111 掩码
如果我们要查询所有 男用户 则:
select * from binary_sample where status & b111000 = b001000;
如果我们要查询所有 管理员用户 则:
select * from binary_sample where status & b111 = b011;
如果我们要查询所有 男管理员用户 则:
select * from binary_sample where status & b111111 = b001011;
如果我们要查询所有 非 新注册未被批准用户 则:
select * from binary_sample where status & b111 != b000;
四,使用PHP程序进行此类计算:
define("USER_NEW",0);//000
define("USER_NORMAL",1);//001
define("USER_ADVANCE",2);//010
define("USER_MANAGE",3);//011
define("USER_SUPER",4);//100
define("USER_MASK",7);//111
define("GENDER_UNKNOWN",0);// 000000
define("GENDER_MALE",8);// 001000
define("GENDER_FEMALE",9);// 010000
define("GENDER_MASK",56);// 111000
如果我们要查询所有 男用户 则:
$status=GENDER_MALE;
$mask=GENDER_MASK;
$sql="select * from binary_sample where status & ${mask} = ${status}";
如果我们要查询所有 管理员用户 则:
$status=USER_MANAGE;
$mask=USER_MASK;
$sql="select * from binary_sample where status & ${mask} = ${status}";
如果我们要查询所有 男管理员用户 则:
$status=GENDER_MALE & USER_MANAGE;
$mask = GENDER_MASK & GENDER_MASK;
$sql="select * from binary_sample where status & ${mask} = ${status}";
如果我们要查询所有 非 新注册未被批准用户 则:
$status = USER_NEW;
$mask = USER_MASK;
$sql="select * from binary_sample where status & ${mask} != ${status}";
依此类推,只要定义好每个值的含义,查询基本上就定了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,

In 2025, global digital virtual currency trading platforms are fiercely competitive. This article authoritatively releases the top ten digital virtual currency trading platforms in the world in 2025 based on indicators such as transaction volume, security, and user experience. OKX ranks first with its strong technical strength and global operation strategy, and Binance follows closely with high liquidity and low fees. Platforms such as Gate.io, Coinbase, and Kraken are at the forefront with their respective advantages. The list covers trading platforms such as Huobi, KuCoin, Bitfinex, Crypto.com and Gemini, each with its own characteristics, but investment should be cautious. To choose a platform, you need to consider factors such as security, liquidity, fees, user experience, currency selection and regulatory compliance, and invest rationally

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

Ranking of the top ten virtual currency trading platforms (latest in 2025): Binance: Global leader, high liquidity, and regulation has attracted attention. OKX: Large user base, supports multiple currencies, and provides leveraged trading. Gate.io: A senior exchange, with a variety of fiat currency payment methods, providing a variety of trading pairs and investment products. Bitget: Derivatives Exchange, high liquidity, low fees. Huobi: An old exchange that supports a variety of currencies and trading pairs. Coinbase: A well-known American exchange, strictly regulated. Phemex and so on.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Top 10 digital currency trading platforms: 1. OKX, 2. Binance, 3. Gate.io, 4. Huobi Global, 5. Kraken, 6. Coinbase, 7. KuCoin, 8. Bitfinex, 9. Crypto.com, 10. Gemini, these exchanges have their own characteristics, and users can choose the platform that suits them based on factors such as security, fees, currency selection, user interface and customer support.

The top four global virtual currency trading platforms in 2025 are: Binance: a leader in the industry, providing diversified trading options and innovative products. OKX: A huge user base, providing comprehensive cryptocurrency services. Gate.io: User-friendly, offering a wide range of cryptocurrency options. Bitget: Focus on derivatives trading and provides high leverage futures contracts.
