mysql数据类型和常用函数

目录

1.整型

1.1参数signed和unsigned

1.2参数zerofill

1.3参数auto_increment

2.数字类型 

2.1floor()向下取整

2.2随机函数rand()

2.3重复函数repeat()

3.字符串类型

3.1length()查看字节长度,char_length()查看字符长度 

3.2字符集

3.2.1查看默认字符集

3.2.2查看已经创建的数据库的字符集

3.2.3修改已经创建的数据库的字符集

3.2.4修改mysql默认字符集

3.2.5两条转换表字符集的语句

3.2.6查看支持的字符集

3.3hex()函数

3.4cast()函数

3.5排序规则collation

3.6对于密码的加密,md5()函数

3.7upper()和lower()函数

3.8concat()和concat_ws()函数

3.9lpad()和rpad()函数

4.enum&set类型

5.时间类型

5.1日期函数

​编辑 5.1.1now()函数

5.1.2now()和sysdate()的区别

5.1.3date_add()和date_sub()函数

5.1.4date_format()函数

6.json类型

6.1json类型相关函数

6.1.1json_extract()函数

 6.1.2json_remove()函数


mysql的数据类型有:整型、数字类型、字符串类型、日期类型、json类型等主要类型

1.整型

1.1参数signed和unsigned

可以创建有符号(signed)和无符号(unsigned)的整型类型 

(root@localhost) [(none)]> use python;
Database changed
(root@localhost) [python]> create table z (a int unsigned,b tinyint signed);
Query OK, 0 rows affected (0.58 sec)(root@localhost) [python]> insert into z values(1,1);
Query OK, 1 row affected (0.13 sec)(root@localhost) [python]> insert into z values(-1,1);
ERROR 1264 (22003): Out of range value for column 'a' at row 1
(root@localhost) [python]> insert into z values(1,-1);
Query OK, 1 row affected (0.05 sec)(root@localhost) [python]> select * from z;
+------+------+
| a    | b    |
+------+------+
|    1 |    1 |
|    1 |   -1 |
+------+------+
2 rows in set (0.00 sec)

1.2参数zerofill

查看创建表ddl有int(10)和tinyint(4),其中10和4代表显示的数值总长度,长度不够用0填充

(root@localhost) [python]> show create table z\G
*************************** 1. row ***************************Table: z
Create Table: CREATE TABLE `z` (`a` int(10) unsigned DEFAULT NULL,`b` tinyint(4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.04 sec)(root@localhost) [python]> alter table z change column a a int unsigned zerofill;
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0(root@localhost) [python]> select * from z;
+------------+------+
| a          | b    |
+------------+------+
| 0000000001 |    1 |
| 0000000001 |   -1 |
+------------+------+
2 rows in set (0.00 sec)(root@localhost) [python]> show create table z\G
*************************** 1. row ***************************Table: z
Create Table: CREATE TABLE `z` (`a` int(10) unsigned zerofill DEFAULT NULL,`b` tinyint(4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

可以修改显示的总长度

(root@localhost) [python]> alter table z change column a a int(4) unsigned zerofill;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0(root@localhost) [python]> select * from z;
+------+------+
| a    | b    |
+------+------+
| 0001 |    1 |
| 0001 |   -1 |
+------+------+
2 rows in set (0.00 sec)(root@localhost) [python]> show create table z\G
*************************** 1. row ***************************Table: z
Create Table: CREATE TABLE `z` (`a` int(4) unsigned zerofill DEFAULT NULL,`b` tinyint(4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

可以正常插入比显示长度更大的值

(root@localhost) [python]> insert into z values(20000,2);
Query OK, 1 row affected (0.00 sec)(root@localhost) [python]> select * from z;
+-------+------+
| a     | b    |
+-------+------+
|  0001 |    1 |
|  0001 |   -1 |
| 20000 |    2 |
+-------+------+
3 rows in set (0.00 sec)

1.3参数auto_increment

设置该参数,主键自动递增,select last_insert_id()查询上一次自增id值是多少,表数据量大主键自增字段建议用bigint类型

(root@localhost) [python]> truncate table z;
Query OK, 0 rows affected (0.13 sec)(root@localhost) [python]> alter table z change column a a int auto_increment primary key;
Query OK, 0 rows affected (0.11 sec)
Records: 0  Duplicates: 0  Warnings: 0(root@localhost) [python]> show create table z\G
*************************** 1. row ***************************Table: z
Create Table: CREATE TABLE `z` (`a` int(11) NOT NULL AUTO_INCREMENT,`b` tinyint(4) DEFAULT NULL,PRIMARY KEY (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)(root@localhost) [python]> insert into z values(null,10);
Query OK, 1 row affected (0.01 sec)(root@localhost) [python]> insert into z values(null,20);
Query OK, 1 row affected (0.01 sec)(root@localhost) [python]> insert into z(b) values(30);
Query OK, 1 row affected (0.05 sec)(root@localhost) [python]> insert into z values(40);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
(root@localhost) [python]> select * from z;
+---+------+
| a | b    |
+---+------+
| 1 |   10 |
| 2 |   20 |
| 3 |   30 |
+---+------+
3 rows in set (0.00 sec)(root@localhost) [python]> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
|                3 |
+------------------+
1 row in set (0.06 sec)

 自增字段值在不重启数据库的情况下不会回溯,只会越来越大

(root@localhost) [python]> delete from z where b=30;
Query OK, 1 row affected (0.06 sec)(root@localhost) [python]> insert into z values(null,30);
Query OK, 1 row affected (0.04 sec)(root@localhost) [python]> select * from z;
+---+------+
| a | b    |
+---+------+
| 1 |   10 |
| 2 |   20 |
| 4 |   30 |
+---+------+
3 rows in set (0.00 sec)

自增字段在删除select last_insert_id()的值后,重启数据库会回溯,查看ddl,AUTO_INCREMENT=7

(root@localhost) [python]> select * from z;
+---+------+
| a | b    |
+---+------+
| 1 |   10 |
| 2 |   20 |
| 5 |   40 |
| 6 |   30 |
+---+------+
4 rows in set (0.00 sec)(root@localhost) [python]> show create table z\G
*************************** 1. row ***************************Table: z
Create Table: CREATE TABLE `z` (`a` int(11) NOT NULL AUTO_INCREMENT,`b` tinyint(4) DEFAULT NULL,PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)(root@localhost) [python]> delete from z where a=6;
Query OK, 1 row affected (0.01 sec)(root@localhost) [python]> delete from z where a=5;
Query OK, 1 row affected (0.01 sec)(root@localhost) [python]> select * from z;
+---+------+
| a | b    |
+---+------+
| 1 |   10 |
| 2 |   20 |
+---+------+
2 rows in set (0.00 sec)

删除a=6、a=5后重启数据库查看ddl,AUTO_INCREMENT=3,此时就回溯了,再次插入数据a=3、a=4就回来了,线上删除的值就又出现了,会乱掉,这个问题在mysql8.0得到解决

(root@localhost) [python]> show create table z\G
*************************** 1. row ***************************Table: z
Create Table: CREATE TABLE `z` (`a` int(11) NOT NULL AUTO_INCREMENT,`b` tinyint(4) DEFAULT NULL,PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)(root@localhost) [python]> select * from z;
+---+------+
| a | b    |
+---+------+
| 1 |   10 |
| 2 |   20 |
+---+------+
2 rows in set (0.00 sec)(root@localhost) [python]> insert into z values(null,30);
Query OK, 1 row affected (0.00 sec)(root@localhost) [python]> insert into z values(null,40);
Query OK, 1 row affected (0.01 sec)(root@localhost) [python]> select * from z;
+---+------+
| a | b    |
+---+------+
| 1 |   10 |
| 2 |   20 |
| 3 |   30 |
| 4 |   40 |
+---+------+
4 rows in set (0.00 sec)(root@localhost) [python]>

mysql8.0解决自增字段回溯问题,AUTO_INCREMENT值持久化

[root@localhost ~]# mysql -S/tmp/mysql.sock80 -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 8.0.35 MySQL Community Server - GPLCopyright (c) 2000, 2023, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.(root@localhost) [(none)]> create database zs;
Query OK, 1 row affected (0.42 sec)(root@localhost) [(none)]> use zs
Database changed
(root@localhost) [zs]> create table z (a bigint auto_increment primary key,b tinyint);
Query OK, 0 rows affected (1.36 sec)(root@localhost) [zs]> show create table z\G;
*************************** 1. row ***************************Table: z
Create Table: CREATE TABLE `z` (`a` bigint NOT NULL AUTO_INCREMENT,`b` tinyint DEFAULT NULL,PRIMARY KEY (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.22 sec)ERROR: 
No query specified(root@localhost) [zs]> insert into z values(null,10);
Query OK, 1 row affected (0.08 sec)(root@localhost) [zs]> insert into z values(null,20);
Query OK, 1 row affected (0.05 sec)(root@localhost) [zs]> insert into z values(null,30);
Query OK, 1 row affected (0.05 sec)(root@localhost) [zs]> insert into z values(null,40);
Query OK, 1 row affected (0.01 sec)(root@localhost) [zs]> show create table z\G;
*************************** 1. row ***************************Table: z
Create Table: CREATE TABLE `z` (`a` bigint NOT NULL AUTO_INCREMENT,`b` tinyint DEFAULT NULL,PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)ERROR: 
No query specified(root@localhost) [zs]> select * from z;
+---+------+
| a | b    |
+---+------+
| 1 |   10 |
| 2 |   20 |
| 3 |   30 |
| 4 |   40 |
+---+------+
4 rows in set (0.01 sec)(root@localhost) [zs]> delete from z where a=4;
Query OK, 1 row affected (0.06 sec)(root@localhost) [zs]> delete from z where a=3;
Query OK, 1 row affected (0.05 sec)(root@localhost) [zs]> select * from z;
+---+------+
| a | b    |
+---+------+
| 1 |   10 |
| 2 |   20 |
+---+------+
2 rows in set (0.00 sec)(root@localhost) [zs]> show create table z\G;
*************************** 1. row ***************************Table: z
Create Table: CREATE TABLE `z` (`a` bigint NOT NULL AUTO_INCREMENT,`b` tinyint DEFAULT NULL,PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)ERROR: 
No query specified(root@localhost) [zs]> exit
Bye
[root@localhost ~]# mysqld_multi stop 80
[root@localhost ~]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld32 is not running
MySQL server from group: mysqld1 is not running
MySQL server from group: mysqld2 is not running
MySQL server from group: mysqld80 is not running
[root@localhost ~]# mysqld_multi start 80
[root@localhost ~]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld32 is not running
MySQL server from group: mysqld1 is not running
MySQL server from group: mysqld2 is not running
MySQL server from group: mysqld80 is running
[root@localhost ~]# mysql -S/tmp/mysql.sock80 -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.35 MySQL Community Server - GPLCopyright (c) 2000, 2023, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.(root@localhost) [(none)]> use zs;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
(root@localhost) [zs]> show create table z\G;
*************************** 1. row ***************************Table: z
Create Table: CREATE TABLE `z` (`a` bigint NOT NULL AUTO_INCREMENT,`b` tinyint DEFAULT NULL,PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)ERROR: 
No query specified(root@localhost) [zs]> select * from z;
+---+------+
| a | b    |
+---+------+
| 1 |   10 |
| 2 |   20 |
+---+------+
2 rows in set (0.00 sec)(root@localhost) [zs]> insert into z values(null,30);
Query OK, 1 row affected (0.01 sec)(root@localhost) [zs]> insert into z values(null,40);
Query OK, 1 row affected (0.00 sec)(root@localhost) [zs]> select * from z;
+---+------+
| a | b    |
+---+------+
| 1 |   10 |
| 2 |   20 |
| 5 |   30 |
| 6 |   40 |
+---+------+
4 rows in set (0.00 sec)(root@localhost) [zs]>

int类型总结

  • 推荐不要使用UNSIGNED
  • 范围本质上没有大的改变
  • UNSIGNED可能会有溢出现象发生
  • 自增INT类型主键建议使用BIGINT

2.数字类型 

一般建议用decimal 

2.1floor()向下取整

(root@localhost) [(none)]> select floor(1.5);
+------------+
| floor(1.5) |
+------------+
|          1 |
+------------+
1 row in set (0.00 sec)(root@localhost) [(none)]> select floor(1.9);
+------------+
| floor(1.9) |
+------------+
|          1 |
+------------+
1 row in set (0.00 sec)(root@localhost) [(none)]> select floor(-1.9);
+-------------+
| floor(-1.9) |
+-------------+
|          -2 |
+-------------+
1 row in set (0.00 sec)

2.2随机函数rand()

(root@localhost) [(none)]> select rand() ;
+-------------------+
| rand()            |
+-------------------+
| 0.590530601880076 |
+-------------------+
1 row in set (0.00 sec)(root@localhost) [(none)]> select rand() ;
+--------------------+
| rand()             |
+--------------------+
| 0.1034116913596277 |
+--------------------+
1 row in set (0.00 sec)

1~100随机整数

(root@localhost) [(none)]> select floor(1+rand()*99);
+--------------------+
| floor(1+rand()*99) |
+--------------------+
|                 74 |
+--------------------+
1 row in set (0.00 sec)(root@localhost) [(none)]> select floor(1+rand()*99);
+--------------------+
| floor(1+rand()*99) |
+--------------------+
|                 42 |
+--------------------+
1 row in set (0.00 sec)(root@localhost) [(none)]> select floor(1+rand()*99);
+--------------------+
| floor(1+rand()*99) |
+--------------------+
|                 85 |
+--------------------+
1 row in set (0.00 sec)

2.3重复函数repeat()

(root@localhost) [(none)]> select repeat(1,3);
+-------------+
| repeat(1,3) |
+-------------+
| 111         |
+-------------+
1 row in set (0.00 sec)(root@localhost) [(none)]> select repeat(32,3);
+--------------+
| repeat(32,3) |
+--------------+
| 323232       |
+--------------+
1 row in set (0.00 sec)(root@localhost) [(none)]> select repeat('b',5);
+---------------+
| repeat('b',5) |
+---------------+
| bbbbb         |
+---------------+
1 row in set (0.00 sec)(root@localhost) [(none)]> select repeat('ba',4);
+----------------+
| repeat('ba',4) |
+----------------+
| babababa       |
+----------------+
1 row in set (0.00 sec)

字符串或数字变长

(root@localhost) [mysql]> select repeat('a',floor(1+rand()*99));
+-----------------------------------------------------------------------------------------------------+
| repeat('a',floor(1+rand()*99))                                                                      |
+-----------------------------------------------------------------------------------------------------+
| aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
+-----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)(root@localhost) [mysql]> select repeat('a',floor(1+rand()*99));
+--------------------------------------------+
| repeat('a',floor(1+rand()*99))             |
+--------------------------------------------+
| aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
+--------------------------------------------+
1 row in set (0.00 sec)(root@localhost) [mysql]> select repeat('b1',floor(1+rand()*20));
+---------------------------------+
| repeat('b1',floor(1+rand()*20)) |
+---------------------------------+
| b1b1b1                          |
+---------------------------------+
1 row in set (0.00 sec)(root@localhost) [mysql]> select repeat('b1',floor(1+rand()*20));
+---------------------------------+
| repeat('b1',floor(1+rand()*20)) |
+---------------------------------+
| b1b1b1b1b1b1b1b1b1              |
+---------------------------------+
1 row in set (0.00 sec)

3.字符串类型

char(n)和varchar(n)中的n代表字符,有字符集

(root@localhost) [python]> drop table z;
Query OK, 0 rows affected (0.72 sec)(root@localhost) [python]> create table z (a char(1),b varchar(3),c binary(1),d varbinary(3));
Query OK, 0 rows affected (0.02 sec)(root@localhost) [python]> insert into z values('a','bbb','c','ddd');
Query OK, 1 row affected (0.01 sec)(root@localhost) [python]> select * from z;
+------+------+------+------+
| a    | b    | c    | d    |
+------+------+------+------+
| a    | bbb  | c    | ddd  |
+------+------+------+------+
1 row in set (0.00 sec)(root@localhost) [python]> show create table z\G
*************************** 1. row ***************************Table: z
Create Table: CREATE TABLE `z` (`a` char(1) DEFAULT NULL,`b` varchar(3) DEFAULT NULL,`c` binary(1) DEFAULT NULL,`d` varbinary(3) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)(root@localhost) [python]> insert into z values('紫','bbc','x','ooo');
ERROR 1366 (HY000): Incorrect string value: '\xE7\xB4\xAB' for column 'a' at row 1
(root@localhost) [python]> alter table z convert to charset utf8mb4;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0(root@localhost) [python]> show create table z\G
*************************** 1. row ***************************Table: z
Create Table: CREATE TABLE `z` (`a` char(1) DEFAULT NULL,`b` varchar(3) DEFAULT NULL,`c` binary(1) DEFAULT NULL,`d` varbinary(3) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)(root@localhost) [python]> insert into z values('紫','bbc','x','ooo');
Query OK, 1 row affected (0.00 sec)(root@localhost) [python]> insert into z values('x','bbc','紫','ooo');
ERROR 1406 (22001): Data too long for column 'c' at row 1

3.1length()查看字节长度,char_length()查看字符长度 

(root@localhost) [python]> select length('紫'),char_length('紫');
+---------------+--------------------+
| length('紫')  | char_length('紫')  |
+---------------+--------------------+
|             3 |                  1 |
+---------------+--------------------+
1 row in set (0.00 sec)

3.2字符集

mysql中的字符集最小是在列级别上

3.2.1查看默认字符集

(root@localhost) [python]> show variables like '%character%';
+--------------------------+----------------------------------------------------------------+
| Variable_name            | Value                                                          |
+--------------------------+----------------------------------------------------------------+
| character_set_client     | utf8                                                           |
| character_set_connection | utf8                                                           |
| character_set_database   | latin1                                                         |
| character_set_filesystem | binary                                                         |
| character_set_results    | utf8                                                           |
| character_set_server     | utf8mb4                                                        |
| character_set_system     | utf8                                                           |
| character_sets_dir       | /usr/local/mysql-5.7.44-linux-glibc2.12-x86_64/share/charsets/ |
+--------------------------+----------------------------------------------------------------+

3.2.2查看已经创建的数据库的字符集

(root@localhost) [zs]> show create database python;
+----------+-------------------------------------------------------------------+
| Database | Create Database                                                   |
+----------+-------------------------------------------------------------------+
| python   | CREATE DATABASE `python` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-------------------------------------------------------------------+
1 row in set (0.00 sec)

3.2.3修改已经创建的数据库的字符集

(root@localhost) [python]> alter database python charset utf8mb4;
Query OK, 1 row affected (0.00 sec)(root@localhost) [python]> show create database python;
+----------+--------------------------------------------------------------------+
| Database | Create Database                                                    |
+----------+--------------------------------------------------------------------+
| python   | CREATE DATABASE `python` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ |
+----------+--------------------------------------------------------------------+
1 row in set (0.00 sec)

3.2.4修改mysql默认字符集

在my.cnf中加入参数

[mysqld]

character_set_server=utf8mb4

3.2.5两条转换表字符集的语句

alter table t charset utf8mb4;与alter table t convert to charset(或者character set) utf8mb4;的不同,前者对已经存在的列不进行字符集转换,只对新增列转换,后者是对已经存在的列和新增列都转换,旧的字符集是新的字符集的子集,在生产上要注意转换字符集是要锁表的,要小心

(root@localhost) [python]> drop table t;
Query OK, 0 rows affected (0.01 sec)(root@localhost) [python]> create table t(a varchar(10),b char(2)) charset gbk;
Query OK, 0 rows affected (0.04 sec)(root@localhost) [python]> show create table t\G;
*************************** 1. row ***************************Table: t
Create Table: CREATE TABLE `t` (`a` varchar(10) DEFAULT NULL,`b` char(2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk
1 row in set (0.00 sec)ERROR: 
No query specified(root@localhost) [python]> alter table t charset utf8mb4;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0(root@localhost) [python]> show create table t\G;
*************************** 1. row ***************************Table: t
Create Table: CREATE TABLE `t` (`a` varchar(10) CHARACTER SET gbk DEFAULT NULL,`b` char(2) CHARACTER SET gbk DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)ERROR: 
No query specified(root@localhost) [python]> alter table t add column c varchar(11);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0(root@localhost) [python]> show create table t\G;
*************************** 1. row ***************************Table: t
Create Table: CREATE TABLE `t` (`a` varchar(10) CHARACTER SET gbk DEFAULT NULL,`b` char(2) CHARACTER SET gbk DEFAULT NULL,`c` varchar(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)ERROR: 
No query specified(root@localhost) [python]> alter table t convert to charset utf8mb4;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0(root@localhost) [python]> show create table t\G;
*************************** 1. row ***************************Table: t
Create Table: CREATE TABLE `t` (`a` varchar(10) DEFAULT NULL,`b` char(2) DEFAULT NULL,`c` varchar(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)ERROR: 
No query specified

3.2.6查看支持的字符集

show charset,Maxlen表示最大的一个字符占用多少个字节

(root@localhost) [python]> show charset;
+----------+---------------------------------+---------------------+--------+
| Charset  | Description                     | Default collation   | Maxlen |
+----------+---------------------------------+---------------------+--------+
| big5     | Big5 Traditional Chinese        | big5_chinese_ci     |      2 |
| dec8     | DEC West European               | dec8_swedish_ci     |      1 |
| cp850    | DOS West European               | cp850_general_ci    |      1 |
| hp8      | HP West European                | hp8_english_ci      |      1 |
| koi8r    | KOI8-R Relcom Russian           | koi8r_general_ci    |      1 |
| latin1   | cp1252 West European            | latin1_swedish_ci   |      1 |
| latin2   | ISO 8859-2 Central European     | latin2_general_ci   |      1 |
| swe7     | 7bit Swedish                    | swe7_swedish_ci     |      1 |
| ascii    | US ASCII                        | ascii_general_ci    |      1 |
| ujis     | EUC-JP Japanese                 | ujis_japanese_ci    |      3 |
| sjis     | Shift-JIS Japanese              | sjis_japanese_ci    |      2 |
| hebrew   | ISO 8859-8 Hebrew               | hebrew_general_ci   |      1 |
| tis620   | TIS620 Thai                     | tis620_thai_ci      |      1 |
| euckr    | EUC-KR Korean                   | euckr_korean_ci     |      2 |
| koi8u    | KOI8-U Ukrainian                | koi8u_general_ci    |      1 |
| gb2312   | GB2312 Simplified Chinese       | gb2312_chinese_ci   |      2 |
| greek    | ISO 8859-7 Greek                | greek_general_ci    |      1 |
| cp1250   | Windows Central European        | cp1250_general_ci   |      1 |
| gbk      | GBK Simplified Chinese          | gbk_chinese_ci      |      2 |
| latin5   | ISO 8859-9 Turkish              | latin5_turkish_ci   |      1 |
| armscii8 | ARMSCII-8 Armenian              | armscii8_general_ci |      1 |
| utf8     | UTF-8 Unicode                   | utf8_general_ci     |      3 |
| ucs2     | UCS-2 Unicode                   | ucs2_general_ci     |      2 |
| cp866    | DOS Russian                     | cp866_general_ci    |      1 |
| keybcs2  | DOS Kamenicky Czech-Slovak      | keybcs2_general_ci  |      1 |
| macce    | Mac Central European            | macce_general_ci    |      1 |
| macroman | Mac West European               | macroman_general_ci |      1 |
| cp852    | DOS Central European            | cp852_general_ci    |      1 |
| latin7   | ISO 8859-13 Baltic              | latin7_general_ci   |      1 |
| utf8mb4  | UTF-8 Unicode                   | utf8mb4_general_ci  |      4 |
| cp1251   | Windows Cyrillic                | cp1251_general_ci   |      1 |
| utf16    | UTF-16 Unicode                  | utf16_general_ci    |      4 |
| utf16le  | UTF-16LE Unicode                | utf16le_general_ci  |      4 |
| cp1256   | Windows Arabic                  | cp1256_general_ci   |      1 |
| cp1257   | Windows Baltic                  | cp1257_general_ci   |      1 |
| utf32    | UTF-32 Unicode                  | utf32_general_ci    |      4 |
| binary   | Binary pseudo charset           | binary              |      1 |
| geostd8  | GEOSTD8 Georgian                | geostd8_general_ci  |      1 |
| cp932    | SJIS for Windows Japanese       | cp932_japanese_ci   |      2 |
| eucjpms  | UJIS for Windows Japanese       | eucjpms_japanese_ci |      3 |
| gb18030  | China National Standard GB18030 | gb18030_chinese_ci  |      4 |
+----------+---------------------------------+---------------------+--------+
41 rows in set (0.00 sec)

3.3hex()函数

查看字符的16进制

(root@localhost) [python]> select hex('a');
+----------+
| hex('a') |
+----------+
| 61       |
+----------+
1 row in set (0.00 sec)(root@localhost) [python]> select hex('ab');
+-----------+
| hex('ab') |
+-----------+
| 6162      |
+-----------+
1 row in set (0.00 sec)(root@localhost) [python]> select hex('abc');
+------------+
| hex('abc') |
+------------+
| 616263     |
+------------+
1 row in set (0.00 sec)(root@localhost) [python]> select hex('紫');
+------------+
| hex('紫')  |
+------------+
| E7B4AB     |
+------------+
1 row in set (0.00 sec)(root@localhost) [python]> select 0xE7B4AB;
+----------+
| 0xE7B4AB |
+----------+
| 紫       |
+----------+
1 row in set (0.00 sec)(root@localhost) [python]> insert into t values('aaa',0xE7B4AB,0xE7B4AB);
Query OK, 1 row affected (0.00 sec)(root@localhost) [python]> select * from t;
+------+------+------+
| a    | b    | c    |
+------+------+------+
| aaa  | 紫   | 紫   |
+------+------+------+
1 row in set (0.00 sec)

3.4cast()函数

CAST函数用于将某种数据类型的表达式显式转换为另一种数据类型。CAST()函数的参数是一个表达式,它包括用AS关键字分隔的源值和目标数据类型。

语法

CAST(value AS datatype)

datatype参数取值 

描述
DATE将value转换成'YYYY-MM-DD'格式
DATETIME将value转换成'YYYY-MM-DD HH:MM:SS'格式
TIME将value转换成'HH:MM:SS'格式
CHAR将value转换成CHAR(固定长度的字符串)格式
SIGNED将value转换成INT(有符号的整数)格式
UNSIGNED将value转换成INT(无符号的整数)格式
DECIMAL将value转换成FLOAT(浮点数)格式 
BINARY将value转换成二进制格式
(root@localhost) [python]> SELECT CAST('13.6' AS decimal(9,2));
+------------------------------+
| CAST('13.6' AS decimal(9,2)) |
+------------------------------+
|                        13.60 |
+------------------------------+
1 row in set (0.00 sec)(root@localhost) [python]> select cast(123 as char(1));
+----------------------+
| cast(123 as char(1)) |
+----------------------+
| 1                    |
+----------------------+
1 row in set, 1 warning (0.00 sec)(root@localhost) [python]> select cast(123 as char(10));
+-----------------------+
| cast(123 as char(10)) |
+-----------------------+
| 123                   |
+-----------------------+
1 row in set (0.00 sec)(root@localhost) [python]> select cast('a' as char(10));
+-----------------------+
| cast('a' as char(10)) |
+-----------------------+
| a                     |
+-----------------------+
1 row in set (0.01 sec)(root@localhost) [python]> select cast('a' as char(10) charset gbk);
+-----------------------------------+
| cast('a' as char(10) charset gbk) |
+-----------------------------------+
| a                                 |
+-----------------------------------+
1 row in set (0.00 sec)(root@localhost) [python]> select hex(cast('a' as char(10) charset gbk));
+----------------------------------------+
| hex(cast('a' as char(10) charset gbk)) |
+----------------------------------------+
| 61                                     |
+----------------------------------------+
1 row in set (0.00 sec)(root@localhost) [python]> select hex('a');
+----------+
| hex('a') |
+----------+
| 61       |
+----------+
1 row in set (0.00 sec)(root@localhost) [python]> select hex(cast('紫' as char(10) charset gbk));
+------------------------------------------+
| hex(cast('紫' as char(10) charset gbk))  |
+------------------------------------------+
| D7CF                                     |
+------------------------------------------+
1 row in set (0.00 sec)(root@localhost) [python]> select hex('紫');
+------------+
| hex('紫')  |
+------------+
| E7B4AB     |
+------------+
1 row in set (0.00 sec)(root@localhost) [python]> select 0xD7CF;
+--------+
| 0xD7CF |
+--------+
| ؏       |
+--------+
1 row in set (0.00 sec)(root@localhost) [python]> show create table z\G;
*************************** 1. row ***************************Table: z
Create Table: CREATE TABLE `z` (`a` char(1) DEFAULT NULL,`b` varchar(3) DEFAULT NULL,`c` binary(1) DEFAULT NULL,`d` varbinary(3) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)ERROR: 
No query specified(root@localhost) [python]> insert into z values('a',0xD7CF,'c',0xD7CF);
ERROR 1366 (HY000): Incorrect string value: '\xD7\xCF' for column 'b' at row 1
(root@localhost) [python]> insert into z values('a',0xE7B4AB,'c',0xD7CF);
Query OK, 1 row affected (0.00 sec)(root@localhost) [python]> select * from z;
+------+------+------+------+
| a    | b    | c    | d    |
+------+------+------+------+
| a    | bbb  | c    | ddd  |
| 紫   | bbc  | x    | ooo  |
| a    | 紫   | c    | ؏     |
+------+------+------+------+
3 rows in set (0.00 sec)

从上面看出,varbinary类型不会检测对应的字符是否存在,只会去存16进制的这个值,varchar类型会去检测16进制的值对应的字符是否存在

3.5排序规则collation

(root@localhost) [(none)]> select 'a' = 'a';
+-----------+
| 'a' = 'a' |
+-----------+
|         1 |
+-----------+
1 row in set (0.00 sec)(root@localhost) [(none)]> select 'a' = 'a    ';
+---------------+
| 'a' = 'a    ' |
+---------------+
|             1 |
+---------------+
1 row in set (0.00 sec)(root@localhost) [(none)]> select 'a' = 'A';
+-----------+
| 'a' = 'A' |
+-----------+
|         1 |
+-----------+
1 row in set (0.01 sec)(root@localhost) [(none)]> select 'a' = 'A    ';
+---------------+
| 'a' = 'A    ' |
+---------------+
|             1 |
+---------------+
1 row in set (0.00 sec)(root@localhost) [python]> drop table t;
Query OK, 0 rows affected (0.02 sec)(root@localhost) [python]> create table t (a char(1),unique key(a));
Query OK, 0 rows affected (0.02 sec)(root@localhost) [python]> insert into t values('a');
Query OK, 1 row affected (0.00 sec)(root@localhost) [python]> insert into t values('A');
ERROR 1062 (23000): Duplicate entry 'A' for key 'a'
(root@localhost) [python]> insert into t values('a    ');
ERROR 1062 (23000): Duplicate entry 'a' for key 'a'
(root@localhost) [python]> insert into t values('A    ');
ERROR 1062 (23000): Duplicate entry 'A' for key 'a'
(root@localhost) [python]> select * from t;
+------+
| a    |
+------+
| a    |
+------+
1 row in set (0.00 sec)

 show charset看到的有一列是Default collation,其中utf8mb4字符集对应的排序规则是utf8mb4_general_ci,ci(case insensitive)表示不区分大小写 

3.6对于密码的加密,md5()函数

(root@localhost) [python]> drop table t;
Query OK, 0 rows affected (0.01 sec)(root@localhost) [python]> create table t(password varchar(128));
Query OK, 0 rows affected (0.01 sec)(root@localhost) [python]> insert into t values('aaa');
Query OK, 1 row affected (0.00 sec)(root@localhost) [python]> insert into t values('BBB');
Query OK, 1 row affected (0.01 sec)(root@localhost) [python]> insert into t values(md5('aaa'));
Query OK, 1 row affected (0.01 sec)(root@localhost) [python]> insert into t values(md5('BBB'));
Query OK, 1 row affected (0.00 sec)(root@localhost) [python]> select * from t;
+----------------------------------+
| password                         |
+----------------------------------+
| aaa                              |
| BBB                              |
| 47bce5c74f589f4867dbd57e9ca9f808 |
| 2bb225f0ba9a58930757a868ed57d9a3 |
+----------------------------------+
4 rows in set (0.00 sec)(root@localhost) [python]> insert into t values(md5(concat('aaa','zs')));
Query OK, 1 row affected (0.00 sec)(root@localhost) [python]> insert into t values(md5(concat('BBB','zs')));
Query OK, 1 row affected (0.01 sec)(root@localhost) [python]> select * from t;
+----------------------------------+
| password                         |
+----------------------------------+
| aaa                              |
| BBB                              |
| 47bce5c74f589f4867dbd57e9ca9f808 |
| 2bb225f0ba9a58930757a868ed57d9a3 |
| ebf93b07e04f07d5efea104226ce36f5 |
| 5812c79db401f78da7fd2aaf03f9ca97 |
+----------------------------------+
6 rows in set (0.00 sec)

密码通过加密存储,一般来说也不区分大小写

强制密码区分大小写

(root@localhost) [python]> create table y (password varchar(128) collate utf8mb4_bin,unique key(password));
Query OK, 0 rows affected (0.01 sec)(root@localhost) [python]> insert into y values('a');
Query OK, 1 row affected (0.00 sec)(root@localhost) [python]> insert into y values('A');
Query OK, 1 row affected (0.00 sec)(root@localhost) [python]> select * from y;
+----------+
| password |
+----------+
| A        |
| a        |
+----------+
2 rows in set (0.00 sec)

3.7upper()和lower()函数

upper()字符串转换成大写,lower()字符串转换成小写

(root@localhost) [python]> select upper('abC');
+--------------+
| upper('abC') |
+--------------+
| ABC          |
+--------------+
1 row in set (0.01 sec)(root@localhost) [python]> select lower('aBC');
+--------------+
| lower('aBC') |
+--------------+
| abc          |
+--------------+
1 row in set (0.01 sec)

3.8concat()和concat_ws()函数

concat()直接把字符串拼接起来,concat_ws()通过指定的符号把字符串拼接起来

(root@localhost) [python]> select concat('a','b','ccc',123);
+---------------------------+
| concat('a','b','ccc',123) |
+---------------------------+
| abccc123                  |
+---------------------------+
1 row in set (0.00 sec)(root@localhost) [python]> select concat_ws('!','a','b','ccc',123);
+----------------------------------+
| concat_ws('!','a','b','ccc',123) |
+----------------------------------+
| a!b!ccc!123                      |
+----------------------------------+
1 row in set (0.00 sec)

3.9lpad()和rpad()函数

lpad()左填充,rpad()右填充

(root@localhost) [python]> select lpad('aaa',8,'!');
+-------------------+
| lpad('aaa',8,'!') |
+-------------------+
| !!!!!aaa          |
+-------------------+
1 row in set (0.00 sec)(root@localhost) [python]> select rpad('aaa',8,'!');
+-------------------+
| rpad('aaa',8,'!') |
+-------------------+
| aaa!!!!!          |
+-------------------+
1 row in set (0.00 sec)

4.enum&set类型

枚举类型常用于性别等distinct的值不多的情况 

(root@localhost) [python]> drop table y;
Query OK, 0 rows affected (0.01 sec)(root@localhost) [python]> create table y (sex enum('male','female'));
Query OK, 0 rows affected (0.01 sec)(root@localhost) [python]> 
(root@localhost) [python]> insert into y values('male');
Query OK, 1 row affected (0.00 sec)(root@localhost) [python]> insert into y values('male11');
ERROR 1265 (01000): Data truncated for column 'sex' at row 1
(root@localhost) [python]> insert into y values('female');
Query OK, 1 row affected (0.00 sec)(root@localhost) [python]> insert into y values('zhs');
ERROR 1265 (01000): Data truncated for column 'sex' at row 1
(root@localhost) [python]> select * from y;
+--------+
| sex    |
+--------+
| male   |
| female |
+--------+
2 rows in set (0.01 sec)

5.时间类型

常用日期类型:datetime和timestamp 

5.1日期函数

 5.1.1now()函数

unix_timestamp(now())是1970-01-01 00:00:00到现在的秒数 

(root@localhost) [dbt3]> select now();
+---------------------+
| now()               |
+---------------------+
| 2024-03-21 13:21:05 |
+---------------------+
1 row in set (0.00 sec)(root@localhost) [dbt3]> select unix_timestamp(now());
+-----------------------+
| unix_timestamp(now()) |
+-----------------------+
|            1710998483 |
+-----------------------+
1 row in set (0.00 sec)(root@localhost) [dbt3]> select timestamp(now());
+---------------------+
| timestamp(now())    |
+---------------------+
| 2024-03-21 13:21:29 |
+---------------------+
1 row in set (0.00 sec)(root@localhost) [dbt3]> create table z (a datetime,b timestamp);
Query OK, 0 rows affected (0.01 sec)(root@localhost) [dbt3]> insert into z values(now(),now());
Query OK, 1 row affected (0.00 sec)(root@localhost) [dbt3]> select * from z;
+---------------------+---------------------+
| a                   | b                   |
+---------------------+---------------------+
| 2024-03-21 13:26:50 | 2024-03-21 13:26:50 |
+---------------------+---------------------+
1 row in set (0.00 sec)(root@localhost) [dbt3]> show variables like 'time_zone';
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| time_zone     | SYSTEM |
+---------------+--------+
1 row in set (0.00 sec)(root@localhost) [dbt3]> set time_zone = '+0:00';
Query OK, 0 rows affected (0.00 sec)(root@localhost) [dbt3]> select * from z;
+---------------------+---------------------+
| a                   | b                   |
+---------------------+---------------------+
| 2024-03-21 13:26:50 | 2024-03-21 05:26:50 |
+---------------------+---------------------+
1 row in set (0.00 sec)(root@localhost) [dbt3]> select now(10);
ERROR 1426 (42000): Too-big precision 10 specified for 'now'. Maximum is 6.
(root@localhost) [dbt3]> select now(6);
+----------------------------+
| now(6)                     |
+----------------------------+
| 2024-03-21 05:29:22.432329 |
+----------------------------+
1 row in set (0.00 sec)(root@localhost) [dbt3]> select now(3);
+-------------------------+
| now(3)                  |
+-------------------------+
| 2024-03-21 05:29:36.677 |
+-------------------------+
1 row in set (0.00 sec)

5.1.2now()和sysdate()的区别

now()表示该条sql执行开始的时间,sysdate()表示执行到该函数的时间

(root@localhost) [dbt3]> select now(),sysdate(),sleep(4),now(),sysdate();
+---------------------+---------------------+----------+---------------------+---------------------+
| now()               | sysdate()           | sleep(4) | now()               | sysdate()           |
+---------------------+---------------------+----------+---------------------+---------------------+
| 2024-03-21 05:34:46 | 2024-03-21 05:34:46 |        0 | 2024-03-21 05:34:46 | 2024-03-21 05:34:50 |
+---------------------+---------------------+----------+---------------------+---------------------+
1 row in set (4.00 sec)(root@localhost) [dbt3]> select now(6),sysdate(6);
+----------------------------+----------------------------+
| now(6)                     | sysdate(6)                 |
+----------------------------+----------------------------+
| 2024-03-21 05:35:58.930067 | 2024-03-21 05:35:58.930234 |
+----------------------------+----------------------------+
1 row in set (0.00 sec)

5.1.3date_add()和date_sub()函数

时间加减函数

(root@localhost) [dbt3]> set time_zone='+8:00';
Query OK, 0 rows affected (0.00 sec)(root@localhost) [dbt3]> select now();
+---------------------+
| now()               |
+---------------------+
| 2024-03-21 13:41:15 |
+---------------------+
1 row in set (0.00 sec)(root@localhost) [dbt3]> select date_add(now(),interval 1 day);
+--------------------------------+
| date_add(now(),interval 1 day) |
+--------------------------------+
| 2024-03-22 13:42:18            |
+--------------------------------+
1 row in set (0.00 sec)(root@localhost) [dbt3]> select date_add(now(),interval -2 day);
+---------------------------------+
| date_add(now(),interval -2 day) |
+---------------------------------+
| 2024-03-19 13:42:33             |
+---------------------------------+
1 row in set (0.00 sec)(root@localhost) [dbt3]> select date_add(now(),interval -2 hour);
+----------------------------------+
| date_add(now(),interval -2 hour) |
+----------------------------------+
| 2024-03-21 11:43:06              |
+----------------------------------+
1 row in set (0.00 sec)(root@localhost) [dbt3]> select date_add(now(),interval -2 minute);
+------------------------------------+
| date_add(now(),interval -2 minute) |
+------------------------------------+
| 2024-03-21 13:41:25                |
+------------------------------------+
1 row in set (0.00 sec)(root@localhost) [dbt3]> select date_sub(now(),interval 2 day);
+--------------------------------+
| date_sub(now(),interval 2 day) |
+--------------------------------+
| 2024-03-19 14:45:05            |
+--------------------------------+
1 row in set (0.00 sec)(root@localhost) [dbt3]> select date_sub(now(),interval -2 day);
+---------------------------------+
| date_sub(now(),interval -2 day) |
+---------------------------------+
| 2024-03-23 14:45:10             |
+---------------------------------+
1 row in set (0.00 sec)(root@localhost) [dbt3]> select date_sub(now(),interval 4 hour);
+---------------------------------+
| date_sub(now(),interval 4 hour) |
+---------------------------------+
| 2024-03-21 10:45:42             |
+---------------------------------+
1 row in set (0.01 sec)

5.1.4date_format()函数

语法

DATE_FORMAT(date,format)

date 参数是合法的日期,format 规定日期/时间的输出格式。 可用的格式如下:

格式描述
%a缩写星期名
%b缩写月名
%c月,数值
%D带有英文前缀的月中的天
%d月的天,数值(00-31)
%e月的天,数值(0-31)
%f微秒
%H小时 (00-23)
%h小时 (01-12)
%I小时 (01-12)
%i分钟,数值(00-59)
%j年的天 (001-366)
%k小时 (0-23)
%l小时 (1-12)
%M月名
%m月,数值(00-12)
%pAM 或 PM
%r时间,12-小时(hh:mm:ss AM 或 PM)
%S秒(00-59)
%s秒(00-59)
%T时间, 24-小时 (hh:mm:ss)
%U周 (00-53) 星期日是一周的第一天
%u周 (00-53) 星期一是一周的第一天
%V周 (01-53) 星期日是一周的第一天,与 %X 使用
%v周 (01-53) 星期一是一周的第一天,与 %x 使用
%W星期名
%w周的天 (0=星期日, 6=星期六)
%X年,其中的星期日是周的第一天,4 位,与 %V 使用
%x年,其中的星期一是周的第一天,4 位,与 %v 使用
%Y年,4 位
%y年,2 位
(root@localhost) [dbt3]> select date_format(now(),'%Y/%m/%s %H.%i.%S');
+----------------------------------------+
| date_format(now(),'%Y/%m/%s %H.%i.%S') |
+----------------------------------------+
| 2024/03/25 13.49.25                    |
+----------------------------------------+
1 row in set (0.00 sec)(root@localhost) [dbt3]> select date_format(now(),'%a');
+-------------------------+
| date_format(now(),'%a') |
+-------------------------+
| Thu                     |
+-------------------------+
1 row in set (0.00 sec)(root@localhost) [dbt3]> select date_format(now(),'%b');
+-------------------------+
| date_format(now(),'%b') |
+-------------------------+
| Mar                     |
+-------------------------+
1 row in set (0.00 sec)(root@localhost) [dbt3]> select date_format(now(),'%W');
+-------------------------+
| date_format(now(),'%W') |
+-------------------------+
| Thursday                |
+-------------------------+
1 row in set (0.00 sec)

6.json类型

 json类型跟python中的字典类似

(root@localhost) [dbt3]> create table userjson(uid bigint auto_increment primary key,data json);
Query OK, 0 rows affected (0.01 sec)(root@localhost) [dbt3]> show create table userjson\G
*************************** 1. row ***************************Table: userjson
Create Table: CREATE TABLE `userjson` (`uid` bigint(20) NOT NULL AUTO_INCREMENT,`data` json DEFAULT NULL,PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)(root@localhost) [dbt3]> INSERT INTO userjson(uid,data)VALUES (NULL,'{"name":"David","address": "Shanghai"}');
Query OK, 1 row affected (0.04 sec)(root@localhost) [dbt3]> INSERT INTO userjson(uid,data)VALUES (NULL,'{"name":"Jim","passport":"E02318883"}');
Query OK, 1 row affected (0.00 sec)(root@localhost) [dbt3]> select * from userjson;
+-----+------------------------------------------+
| uid | data                                     |
+-----+------------------------------------------+
|   1 | {"name": "David", "address": "Shanghai"} |
|   2 | {"name": "Jim", "passport": "E02318883"} |
+-----+------------------------------------------+
2 rows in set (0.00 sec)

6.1json类型相关函数

6.1.1json_extract()函数

(root@localhost) [dbt3]> select uid,json_extract(data,'$.name') from userjson;
+-----+-----------------------------+
| uid | json_extract(data,'$.name') |
+-----+-----------------------------+
|   1 | "David"                     |
|   2 | "Jim"                       |
+-----+-----------------------------+
2 rows in set (0.00 sec)(root@localhost) [dbt3]> select uid,json_unquote(json_extract(data,'$.name')) from userjson;
+-----+-------------------------------------------+
| uid | json_unquote(json_extract(data,'$.name')) |
+-----+-------------------------------------------+
|   1 | David                                     |
|   2 | Jim                                       |
+-----+-------------------------------------------+
2 rows in set (0.00 sec)(root@localhost) [dbt3]> select uid,data->'$.name' from userjson;
+-----+----------------+
| uid | data->'$.name' |
+-----+----------------+
|   1 | "David"        |
|   2 | "Jim"          |
+-----+----------------+
2 rows in set (0.00 sec)(root@localhost) [dbt3]> select uid,data->>'$.name' from userjson;
+-----+-----------------+
| uid | data->>'$.name' |
+-----+-----------------+
|   1 | David           |
|   2 | Jim             |
+-----+-----------------+
2 rows in set (0.00 sec)(root@localhost) [dbt3]> select uid,data->>'$.passport' from userjson;
+-----+---------------------+
| uid | data->>'$.passport' |
+-----+---------------------+
|   1 | NULL                |
|   2 | E02318883           |
+-----+---------------------+
2 rows in set (0.00 sec)

 6.1.2json_remove()函数

(root@localhost) [dbt3]> select * from userjson;
+-----+------------------------------------------+
| uid | data                                     |
+-----+------------------------------------------+
|   1 | {"name": "David", "address": "Shanghai"} |
|   2 | {"name": "Jim", "passport": "E02318883"} |
+-----+------------------------------------------+
2 rows in set (0.00 sec)(root@localhost) [dbt3]> select uid,json_remove(data,'$.name') from userjson;
+-----+----------------------------+
| uid | json_remove(data,'$.name') |
+-----+----------------------------+
|   1 | {"address": "Shanghai"}    |
|   2 | {"passport": "E02318883"}  |
+-----+----------------------------+
2 rows in set (0.01 sec)(root@localhost) [dbt3]> select uid,json_remove(data,'$.passport') from userjson;
+-----+------------------------------------------+
| uid | json_remove(data,'$.passport')           |
+-----+------------------------------------------+
|   1 | {"name": "David", "address": "Shanghai"} |
|   2 | {"name": "Jim"}                          |
+-----+------------------------------------------+
2 rows in set (0.00 sec)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/760347.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

海外舆情监控的重要性以及云手机的推荐

海外市场不仅有着无尽的商机,同时也存在着各种各样的挑战。例如,海外市场舆情的复杂变化给企业带来了潜在风险,尤其在新媒体技术快速发展的时代,舆论对企业品牌形象的影响日益巨大。本文将详细探讨海外舆情监控的重要性以及为大家…

FlorisBoard:Android开源键盘的现代化选择

FlorisBoard:Android开源键盘的现代化选择 简介 FlorisBoard是一款免费且开源的安卓键盘,适用于Android 7.0及以上版本的设备。它的现代化设计和用户友好的界面使其在众多键盘应用中脱颖而出。FlorisBoard的独特之处在于它注重用户体验的同时&#xff0…

抖音视频提取工具可导出视频分享链接|视频爬虫批量下载

抖音无水印视频批量下载工具操作指南 一、工具介绍 我们开发了一款功能强大的视频批量下载工具,不仅能够通过单个视频链接进行提取,还可以通过关键词进行视频搜索,Q:290615413实现批量提取和有选择性的下载。 二、操作步骤 2.1 关键词批量…

IO网络通信

IO Bio: 同步阻塞,一个线程一次连接,基于字符流/字节流 Nio:同步非阻塞 一个线程多个连接,多个请求注册到多路复用selector上,多路复用器轮训连接 面向缓冲区, 从某通道读取数据到缓存区&…

Android:adb命令

执行adb命令的窗口如下 Mac或Linux系统里的终端窗口; window系统运行输入cmd打开的指令窗口; Android Studio 里控制下面的Terminal窗口 1. 查看已链接的设备和模拟器 adb devices -l 2. 查看Android内核版本号 adb shell getprop ro.build.version.re…

数据结构和算法:哈希表

哈希表 哈希表(hash table),又称散列表,它通过建立键 key 与值 value 之间的映射,实现高效的元素查询。具体而言,向哈希表中输入一个键 key ,则可以在 𝑂(1) 时间内获取对应的值 va…

CSS样式表 样式优先级 选择器以及选择器的权重优先级

CSS组成 css由选择符和声明组成,声明又分为属性和属性值 属性必须放在花括号里面,属性与属性值必用冒号连接 每条声明用分号结束 当一个属性有多个属性值的时候,属性值与属性值部分先后顺序,用空格隔开 在书写样式过程中、空格、换…

软文营销塑造品牌形象,迅推客助力企业成功求发展

企业可以选择适合自身的新闻宣传方法。迅推客作为互联网整合营销机构,积极推动软文营销,助力企业塑造高端品牌形象。 1、新闻稿战略规划。 新闻宣传前要有明确的战略规划。在新闻稿的创作过程中,企业的需求与消费者的需求巧妙地融合在一起。…

室内3D模型选择指南:哪家更值得信赖?

如今室内设计的3D模型已经成为了一种重要的设计工具。它们能够帮助设计师们更好地向客户展示他们的设计想法,同时也让客户能够更直观地理解他们的需求。然而,面对市场上众多的室内3D模型网站,我们该如何选择呢? 室内3D模型选择指南&#xff…

echarts系列:echarts增加loading

背景 最近在大屏项目研发时,存在多个echarts图表,有些接口获取数据较慢,需要加loading样式; 解决方案 翻阅api,找到showLoading可进行设置 参考: 加载动画效果 显示加载动画效果。可以在加载数据前手动…

10个优秀的Github开源项目

1Panel 是一个现代化、开源的 Linux 服务器运维管理面板 EX-chatGPT-精准搜索工具 feishu-chatgpt-飞一般的工作体验工具 Knife4j-是一个集Swagger2 和 OpenAPI3为一体的增强解决方案 Kooder 是 Gitee 团队开发的一个代码搜索系统 mtbird 是一款低代码可视化页面生成器 S…

docker一键部署若依前后端分离版本

比如这里把文件放到/xin/docker/jiaoZ/的目录下,jar包和下面的配置文件都放在这个文件夹下。 注意要把jar端口改为你实际启动的,映射端口也可以改为你想要的。 这里的映射端口为:nginx监听80端口,jar在8620端口,mysq…

Emotion Prompt-LLM能够理解并能通过情感刺激得以增强

Large Language Models Understand and Can be Enhanced by Emotional Stimuli 情感智能对我们的日常行为和互动产生了显著的影响。尽管大型语言模型(LLMs)被视为向人工通用智能迈进的一大步,在许多任务中表现出色,但目前尚不清楚…

matlab 条件数的倒数

目录 一、概述1、算法概述2、主要函数3、参考文献二、条件设置错误的矩阵的敏感度三、求解单位矩阵的条件四、参考链接本文由CSDN点云侠原创,原文链接。如果你不是在点云侠的博客中看到该文章,那么此处便是不要脸的爬虫。 一、概述 1、算法概述

.locked勒索病毒是什么,企业数据被加密了如何恢复?

.locked勒索病毒介绍 .locked勒索病毒是一种恶意软件,它利用加密技术锁定用户的数据或系统,并以此进行勒索。用户一旦感染此病毒,将无法访问其重要文件,病毒会要求用户支付一笔赎金以获取解密密钥。这种病毒通常使用强大的加密算法…

PMP证书可以挂靠?

PMP项目管理是由美国项目管理协会(PMI)提出的,针对项目管理专业人士的资格认证。它被公认为全球项目管理领域含金量最高的认证,与MBA、MPA并列为三大就业金字招牌。PMP认证在全球190多个国家及地区得到高度认可。通过PMP认证考试可…

面试常问:为什么 Vite 速度比 Webpack 快

前言 最近作者在学习 webpack 相关的知识,之前一直对这个问题不是特别了解,甚至讲不出个123....,这个问题在面试中也是常见的,作者在学习的过程当中总结了以下几点,在这里分享给大家看一下,当然最重要的是…

什么是Java语言的反射机制?

什么是反射 反射(Reflection)主要是指程序可以访问、检测和修改它本身状态或行为的一种能力。(就像照镜子反射一样) Java反射机制是指在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法&…

docker -compose运行时端口被占用异常

解决方法:在docker-compose.yml文件中去掉端口的配置(去掉下图圈住部分),我们要启动5个,配置了端口我们只能启动一个,所以就会报错

Android Studio实现内容丰富的安卓教学学习平台

获取源码请点击文章末尾QQ名片联系,源码不免费,尊重创作,尊重劳动 项目编号088 1.开发环境 android stuido3.6 jak1.8 eclipse mysql tomcat 2.功能介绍 安卓端: 1.注册登录 2.查看教学视频资料 3.播放教学视频 4.浏览作业&#…