MySQL函数,常用类型转换函数总结
1、Concat函数。
连接字符串常用:concat函数。如sql查询条件的like查询, AND c.name like concat(#{param.name},'%')
将Int 转为varchar经常用 concat函数,比如concat(8,'0') 得到字符串 '80'
2、Cast函数;CONVERT函数。
用法:CAST(expr AS type), CONVERT(expr,type) , CONVERT(expr USING transcoding_name).
SELECT CONVERT('abc' USING utf8);
将varchar 转为Int 用 cast(str as unsigned) str为varchar类型的字符串 。
比如常用的百分比转换:
select cast((1/3)*100 as UNSIGNED) as percent from dual;
result: 33
MySQL类型转换函数参数 : CAST(xxx AS 类型) , CONVERT(xxx,类型)
这个类型 可以是以下值其中的 一个:
BINARY[(N)]
CHAR[(N)]
DATE
DATETIME
DECIMAL
SIGNED [INTEGER]
TIME
UNSIGNED [INTEGER]
整数 : SIGNED
无符号整数 : UNSIGNED
二进制,同带binary前缀的效果 : BINARY
字符型,可带参数 : CHAR()
日期 : DATE
时间: TIME
日期时间型 : DATETIME
浮点数 : DECIMAL
BINARY str 是CAST(str AS BINARY)的缩略形式:
mysql> SELECT BINARY 'a' = 'A';
-> 0
3、IF函数
mysql中if是函数而不是命令
IF(expr1,expr2,expr3)
如果 expr1 为真(expr1 0 以及 expr1 NULL),那么 IF() 返回 expr2,否则返回 expr3。IF() 返回一个数字或字符串,这取决于它被使用的语境:
mysql> SELECT IF(1>2,2,3);
-> 3
mysql> SELECT IF(1
-> 'yes'
mysql> SELECT IF(STRCMP('test','test1'),'no','yes');
-> 'no'
如果 expr2 或 expr3 明确地为 NULL,那么函数 IF() 的返回值类型为非 NULL 列的类型。(这在选择在 MySQL 4.0.3 中新加入)。 expr1 是作为一个整数值被计算的,这就意味着,如果测试的是一个浮点型或字符串值,就必须进行比较操作:
mysql> SELECT IF(0.1,1,0);
-> 0
mysql> SELECT IF(0.10,1,0);
-> 1
在上面第一种情况下,IF(0.1) 返回 0,是因为 0.1 被转换为一个整数值,返回 IF(0) 的测试结果。这可能不是你所期望的。在第二种情况下,比较测试原浮点数是否为一个非零值。比较的结果被作为整数使用。 缺省的 IF() 返回值类型 (当结果存储在临时表中时,这是非常重要的) 在 MySQL 3.23 中按下列方式确定: 表达式 返回值
表达式(expr2)或表达式(expr3)返回值为字符串 字符串
表达式(expr2)或表达式(expr3)返回值为浮点型值 浮点型
表达式(expr2)或表达式(expr3)返回值为整型 整型
如果表达式(expr2)和表达式(expr3)均是字符串,同时两个字符串均是忽略字母大小写的,那么返回值也是忽略字母大小写的(从 MySQL 3.23.51 开始)。

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.


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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools
