search
HomeDatabaseMysql TutorialMySQL(基础篇)之MySQL(系统函数、加密函数和条件判断函数)

一、MySQL系统函数 VERSION()函数返回数据库的版本号; CONNECTION_ID()函数返回服务器的连接数,也就是到现在为止MySQL服务的连接次数; DATABASE()和SCHEMA()返回当前数据库名。 USER()、SYSTEM_USER()、SESSION_USER()、CURRENT_USER()和CURRENT_USER这几

一、MySQL系统函数

VERSION()函数返回数据库的版本号;

MySQL(基础篇)之MySQL(系统函数、加密函数和条件判断函数)
CONNECTION_ID()函数返回服务器的连接数,也就是到现在为止MySQL服务的连接次数;

MySQL(基础篇)之MySQL(系统函数、加密函数和条件判断函数)
DATABASE()和SCHEMA()返回当前数据库名。

MySQL(基础篇)之MySQL(系统函数、加密函数和条件判断函数)
USER()、SYSTEM_USER()、SESSION_USER()、CURRENT_USER()和CURRENT_USER这几个函数可以返回当前用户的名称

MySQL(基础篇)之MySQL(系统函数、加密函数和条件判断函数)
CHARSET(str)函数返回字符串str的字符集,一般情况这个字符集就是系统的默认字符集;

MySQL(基础篇)之MySQL(系统函数、加密函数和条件判断函数)
COLLATION(str)函数返回字符串str的字符排列方式。

MySQL(基础篇)之MySQL(系统函数、加密函数和条件判断函数)


LAST_INSERT_ID()函数返回最后生成的AUTO_INCREMENT值。

MySQL(基础篇)之MySQL(系统函数、加密函数和条件判断函数)


二、加密函数

PASSWORD(str)函数可以对字符串str进行加密。一般情况下,PASSWORD(str)函数主要是用来给用户的密码加密的。

MySQL(基础篇)之MySQL(系统函数、加密函数和条件判断函数)
MD5(str)函数可以对字符串str进行加密。MD5(str)函数主要对普通的数据进行加密。

MySQL(基础篇)之MySQL(系统函数、加密函数和条件判断函数)
ENCODE(str,pswd_str)函数可以使用字符串pswd_str来加密字符串str。加密的结果是一个二进制数,必须使用BLOB类型的字段来保存它。
DECODE(crypt_str,pswd_str)函数可以使用字符串pswd_str来为crypt_str


三、条件判断函数

if语句

if(expr,v1,v2)函数:如果表达式expr成立,返回结果v1,否则返回结果v2。


select id,grdade,if(grade>=60,'pass','fail') from t;


从表t中查询出id和成绩,当成绩>=60时显示“pass”,否则显示“fail”。




ifnull(v1,v2)函数:如果v1不为空,就显示v1,否则就显示v2的值


select i,iffull(grade,'no grade') from t;


从t表中查询学号,分数,如果分数不为空,显示分数,否则,显示“no grade”。




case语句


例子:


select id,grade,


case when grade>60 then 'good'when grade=60 then 'pass'else 'fail' end level


from t;


例子


select id,grade,


case grade when 90 then 'good'when 60 then 'pass'


when 50 then 'fail' else 'no grade' end level


from t;



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
How does MySQL handle concurrency compared to other RDBMS?How does MySQL handle concurrency compared to other RDBMS?Apr 29, 2025 am 12:44 AM

MySQLhandlesconcurrencyusingamixofrow-levelandtable-levellocking,primarilythroughInnoDB'srow-levellocking.ComparedtootherRDBMS,MySQL'sapproachisefficientformanyusecasesbutmayfacechallengeswithdeadlocksandlacksadvancedfeatureslikePostgreSQL'sSerializa

How does MySQL handle transactions compared to other relational databases?How does MySQL handle transactions compared to other relational databases?Apr 29, 2025 am 12:37 AM

MySQLhandlestransactionseffectivelyusingtheInnoDBengine,supportingACIDpropertiessimilartoPostgreSQLandOracle.1)MySQLusesREPEATABLEREADasthedefaultisolationlevel,whichcanbeadjustedtoREADCOMMITTEDforhigh-trafficscenarios.2)Itoptimizesperformancewithabu

What are the data types available in MySQL?What are the data types available in MySQL?Apr 29, 2025 am 12:28 AM

MySQL data types are divided into numerical, date and time, string, binary and spatial types. Selecting the correct type can optimize database performance and data storage.

What are some best practices for writing efficient SQL queries in MySQL?What are some best practices for writing efficient SQL queries in MySQL?Apr 29, 2025 am 12:24 AM

Best practices include: 1) Understanding the data structure and MySQL processing methods, 2) Appropriate indexing, 3) Avoid SELECT*, 4) Using appropriate JOIN types, 5) Use subqueries with caution, 6) Analyzing queries with EXPLAIN, 7) Consider the impact of queries on server resources, 8) Maintain the database regularly. These practices can make MySQL queries not only fast, but also maintainability, scalability and resource efficiency.

How does MySQL differ from PostgreSQL?How does MySQL differ from PostgreSQL?Apr 29, 2025 am 12:23 AM

MySQLisbetterforspeedandsimplicity,suitableforwebapplications;PostgreSQLexcelsincomplexdatascenarioswithrobustfeatures.MySQLisidealforquickprojectsandread-heavytasks,whilePostgreSQLispreferredforapplicationsrequiringstrictdataintegrityandadvancedSQLf

How does MySQL handle data replication?How does MySQL handle data replication?Apr 28, 2025 am 12:25 AM

MySQL processes data replication through three modes: asynchronous, semi-synchronous and group replication. 1) Asynchronous replication performance is high but data may be lost. 2) Semi-synchronous replication improves data security but increases latency. 3) Group replication supports multi-master replication and failover, suitable for high availability requirements.

How can you use the EXPLAIN statement to analyze query performance?How can you use the EXPLAIN statement to analyze query performance?Apr 28, 2025 am 12:24 AM

The EXPLAIN statement can be used to analyze and improve SQL query performance. 1. Execute the EXPLAIN statement to view the query plan. 2. Analyze the output results, pay attention to access type, index usage and JOIN order. 3. Create or adjust indexes based on the analysis results, optimize JOIN operations, and avoid full table scanning to improve query efficiency.

How do you back up and restore a MySQL database?How do you back up and restore a MySQL database?Apr 28, 2025 am 12:23 AM

Using mysqldump for logical backup and MySQLEnterpriseBackup for hot backup are effective ways to back up MySQL databases. 1. Use mysqldump to back up the database: mysqldump-uroot-pmydatabase>mydatabase_backup.sql. 2. Use MySQLEnterpriseBackup for hot backup: mysqlbackup--user=root-password=password--backup-dir=/path/to/backupbackup. When recovering, use the corresponding life

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools