mysql> SELECT JSON_TYPE('hello'); ERROR 3146 (22032): Invalid data type for JSON data in argument 1 to function json_type; a JSON string or JSON type is required.
mysql> select JSON_TYPE('true'); +----------------------+ | JSON_TYPE('true') | +----------------------+ | BOOLEAN | +----------------------+ mysql> select JSON_TYPE('1'); +----------------------+ | JSON_TYPE('1') | +----------------------+ | INTEGER | +----------------------+ mysql> select JSON_TYPE('2019-01-01'); ERROR 3141 (22032): Invalid JSON text in argument 1 to function json_type: "The document root must not be followed by other values." at position 4.
--官网中说这个会无效,实际测试中是有效的。 mysql> INSERT INTO facts VALUES > (JSON_OBJECT("mascot", "Our mascot is a dolphin named \"Sakila\"."));
--官网中说需要使用这个,实际测试时却报错。 mysql> INSERT INTO facts VALUES > ('{"mascot": "Our mascot is a dolphin named \\"Sakila\\"."}'); Unknown command '\\'. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\\"."))' at line 1
mysql> SELECT sentence FROM facts; +---------------------------------------------------------+ | sentence | +---------------------------------------------------------+ | {"mascot": "Our mascot is a dolphin named \"Sakila\"."} | +---------------------------------------------------------+ 1 row in set (0.00 sec)
mysql> SELECT col->"$.mascot" FROM qtest; ERROR 1146 (42S02): Table 'test.qtest' doesn't exist mysql> SELECT sentence->"$.mascot" FROM facts; +---------------------------------------------+ | sentence->"$.mascot" | +---------------------------------------------+ | "Our mascot is a dolphin named \"Sakila\"." | +---------------------------------------------+ 1 row in set (0.00 sec)
mysql> SELECT sentence->>"$.mascot" FROM facts; +-----------------------------------------+ | sentence->>"$.mascot" | +-----------------------------------------+ | Our mascot is a dolphin named "Sakila". | +-----------------------------------------+ 1 row in set (0.00 sec)
其实如果要简单点就是用单引号(‘)。
1 2
mysql> INSERT INTO facts VALUES(JSON_OBJECT('mascot', 'Our mascot is a dolphin named "Sakila".')); Query OK, 1 row affected (0.00 sec)