bitsCN.com
Mysql可以通过运算符来对表中数据进行运算,比如通过出生日期求年龄等
运算符包括四类,分别是:算数运算符、比较运算符、逻辑运算符和位运算符
算数运算符
加、减、乘运算
mysql> select a,a+5,a*2 from t1;+------+------+------+| a | a+5 | a*2 |+------+------+------+| 24 | 29 | 48 |+------+------+------+1 row in set (0.00 sec)
这里的原值为24,后面也可以使用混合运算,只需要注意优先级即可
除法和取模运算
mysql> select a,a/3,a p 3,a%5,mod(a,5) from t1;+------+--------+---------+------+----------+| a | a/3 | a p 3 | a%5 | mod(a,5) |+------+--------+---------+------+----------+| 24 | 8.0000 | 8 | 4 | 4 |+------+--------+---------+------+----------+1 row in set (0.00 sec)
此处 / 和 p 代表整除,% 和 mod 代表取模
要注意的是,如果被除数为0,那么计算结果是NULL
比较运算符
数值比较
mysql> select a,a=24,a40,a>=24,a24,a24 from t1;+------+------+------+------+-------+-------+-------+-------+--------+| a | a=24 | a40 | a>=24 | a24 | a24 |+------+------+------+------+-------+-------+-------+-------+--------+| 24 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 |+------+------+------+------+-------+-------+-------+-------+--------+1 row in set (0.00 sec)
这里的1代表真,0代表假,需要说明的是和
代表不等于,等同于!=
代表等于,等同于=
此外,等于和不等于不仅可以比较数值,还能比较字符串
字符串比较
mysql> select a,a='24','ha''ha','xa'='xa','b'!='b' from t1;+------+--------+------------+-----------+----------+| a | a='24' | 'ha''ha' | 'xa'='xa' | 'b'!='b' |+------+--------+------------+-----------+----------+| 24 | 1 | 0 | 1 | 0 |+------+--------+------------+-----------+----------+1 row in set (0.00 sec)
is null 和is not null
mysql> select a,a is null, a is not null from t1;+------+-----------+---------------+| a | a is null | a is not null |+------+-----------+---------------+| 24 | 0 | 1 |+------+-----------+---------------+1 row in set (0.00 sec)
这里可以判断是否为空,NULL也可以跟NULL比较
between and和not between and
mysql> select a,a between 15 and 30,a not between 15 and 30 from t1;+------+---------------------+-------------------------+| a | a between 15 and 30 | a not between 15 and 30 |+------+---------------------+-------------------------+| 24 | 1 | 0 |+------+---------------------+-------------------------+1 row in set (0.00 sec)
between and 和not between and可以判断数值是否在某一区间内
in
mysql> select a,a in(1,2,23),a in(24,12,22) from t1;+------+--------------+----------------+| a | a in(1,2,23) | a in(24,12,22) |+------+--------------+----------------+| 24 | 0 | 1 |+------+--------------+----------------+1 row in set (0.00 sec)
判断操作数是否在某一集合内
like
mysql> select s,s like 'beijing',s like 'b%g',s like 'bei____',s like '%jing' from t2;+---------+------------------+--------------+------------------+----------------+| s | s like 'beijing' | s like 'b%g' | s like 'bei____' | s like '%jing' |+---------+------------------+--------------+------------------+----------------+| beijing | 1 | 1 | 1 | 1 |+---------+------------------+--------------+------------------+----------------+1 row in set (0.00 sec)
like可以用来匹配字符串,_代表单个字符,%代表多个字符
逻辑运算符
与运算
mysql> select 2&&2,2&&null,2 and 3,2 and 2;+------+---------+---------+---------+| 2&&2 | 2&&null | 2 and 3 | 2 and 2 |+------+---------+---------+---------+| 1 | NULL | 1 | 1 |+------+---------+---------+---------+1 row in set (0.00 sec)
这里&&和and意思一样
或运算
mysql> select 2||2,2||null,2 or 3,2 or 0;+------+---------+--------+--------+| 2||2 | 2||null | 2 or 3 | 2 or 0 |+------+---------+--------+--------+| 1 | 1 | 1 | 1 |+------+---------+--------+--------+1 row in set (0.00 sec)
这里||和or的意思一样
非运算
mysql> select !1,!2,!null;+----+----+-------+| !1 | !2 | !null |+----+----+-------+| 0 | 0 | NULL |+----+----+-------+1 row in set (0.00 sec)
此外还有位运算,目前还没用到,等用到的时候再补上
bitsCN.com

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

When selecting MySQL's BLOB and TEXT data types, BLOB is suitable for storing binary data, and TEXT is suitable for storing text data. 1) BLOB is suitable for binary data such as pictures and audio, 2) TEXT is suitable for text data such as articles and comments. When choosing, data properties and performance optimization must be considered.

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQLstringdatatypesshouldbechosenbasedondatacharacteristicsandusecases:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseBINARYorVARBINARYforbinarydatalikecryptographickeys.4)UseBLOBorTEXTforlargeuns


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

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
