MySQL数据库基本知识学习
1.注释风格
1 2 3 4 5 6 7 8 9
| # URL编码%23 -- - / --+ (--后面要跟一个或者多个空格) /*...*/ /*!...*/ 内敛注释 ;%00
注: "#"在URL中被认为是锚点,想要使用需要进行URL编码 "+"在URL中会被当作空格,因此可以使用--+来进行注释
|
2.常见函数介绍
2.1关于数据库基本信息函数
1 2 3 4 5 6 7 8 9 10 11
| MariaDB [security]> select user(); +----------------+ | user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec)
MariaDB [security]>
//当前连接数据库用户 root
|
1 2 3 4 5 6 7 8 9
| MariaDB [security]> select database(); +------------+ | database() | +------------+ | security | +------------+ 1 row in set (0.00 sec)
MariaDB [security]>
|
- version()/@@version查看当前数据库版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| MariaDB [security]> select @@version; +----------------+ | @@version | +----------------+ | 5.5.65-MariaDB | +----------------+ 1 row in set (0.00 sec)
MariaDB [security]> select version(); +----------------+ | version() | +----------------+ | 5.5.65-MariaDB | +----------------+ 1 row in set (0.00 sec)
MariaDB [security]>
|
1 2 3 4 5 6 7 8 9
| MariaDB [security]> select @@datadir; +-----------------+ | @@datadir | +-----------------+ | /var/lib/mysql/ | +-----------------+ 1 row in set (0.00 sec)
MariaDB [security]>
|
- @@version_compile_os查看当前使用操作系统
1 2 3 4 5 6 7 8 9
| MariaDB [security]> select @@version_compile_os; +----------------------+ | @@version_compile_os | +----------------------+ | Linux | +----------------------+ 1 row in set (0.00 sec)
MariaDB [security]>
|
2.2字符串连接函数
CONCAT()函数需要一个或多个字符串参数,并将它们连接成一个字符串。CONCAT()函数需要至少一个参数,否则会引起错误。
没有分隔符的连接字符串。
1 2 3 4 5 6 7 8 9
| MariaDB [security]> select concat('a','b','c'); +---------------------+ | concat('a','b','c') | +---------------------+ | abc | +---------------------+ 1 row in set (0.00 sec)
MariaDB [security]>
|
如果添加NULL值,则CONCAT函数将返回一个NULL值,如下所示:
1 2 3 4 5 6 7 8 9
| MariaDB [security]> select concat('a',null,'c'); +----------------------+ | concat('a',null,'c') | +----------------------+ | NULL | +----------------------+ 1 row in set (0.00 sec)
MariaDB [security]>
|
CONCAT_WS(seperator,string1,string2, … );
使用分隔符连接字符串,第一个字段seperator为分隔符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| MariaDB [security]> select concat_ws('~','1','2','3'); +----------------------------+ | concat_ws('~','1','2','3') | +----------------------------+ | 1~2~3 | +----------------------------+ 1 row in set (0.00 sec)
MariaDB [security]> select concat_ws(0x7e,'1','2','3'); +-----------------------------+ | concat_ws(0x7e,'1','2','3') | +-----------------------------+ | 1~2~3 | +-----------------------------+ 1 row in set (0.00 sec)
MariaDB [security]>
|
将多行的查询结果以逗号为分隔符连接成一行结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| MariaDB [security]> select * from ceshi; +----+------+ | id | user | +----+------+ | 1 | haha | | 2 | hehe | +----+------+ 2 rows in set (0.00 sec)
MariaDB [security]> select group_concat(id,user) from ceshi; +-----------------------+ | group_concat(id,user) | +-----------------------+ | 1haha,2hehe | +-----------------------+ 1 row in set (0.00 sec)
MariaDB [security]>
|