Mysql method of recursively querying all data: first create the table and initialize the data; then use the [find_in_set()] function and the [group_concat()] function to implement the recursive query. The code is [SELECT queryChildrenAreaInfo(1); 】.
More related free learning recommendations: mysql tutorial(video)
mysql recursive query method for all data:
1. Create table
DROP TABLE IF EXISTS `t_areainfo`; CREATE TABLE `t_areainfo` ( `id` int(11) NOT '0' AUTO_INCREMENT, `level` int(11) DEFAULT '0', `name` varchar(255) DEFAULT '0', `parentId` int(11) DEFAULT '0', `status` int(11) DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=65 DEFAULT CHARSET=utf8;
2. Initial data
INSERT INTO `t_areainfo` VALUES ('1', '0', '中国', '0', '0'); INSERT INTO `t_areainfo` VALUES ('2', '0', '华北区', '1', '0'); INSERT INTO `t_areainfo` VALUES ('3', '0', '华南区', '1', '0'); INSERT INTO `t_areainfo` VALUES ('4', '0', '北京', '2', '0'); INSERT INTO `t_areainfo` VALUES ('5', '0', '海淀区', '4', '0'); INSERT INTO `t_areainfo` VALUES ('6', '0', '丰台区', '4', '0'); INSERT INTO `t_areainfo` VALUES ('7', '0', '朝阳区', '4', '0'); INSERT INTO `t_areainfo` VALUES ('8', '0', '北京XX区1', '4', '0'); INSERT INTO `t_areainfo` VALUES ('9', '0', '北京XX区2', '4', '0'); INSERT INTO `t_areainfo` VALUES ('10', '0', '北京XX区3', '4', '0'); INSERT INTO `t_areainfo` VALUES ('11', '0', '北京XX区4', '4', '0'); INSERT INTO `t_areainfo` VALUES ('12', '0', '北京XX区5', '4', '0'); INSERT INTO `t_areainfo` VALUES ('13', '0', '北京XX区6', '4', '0'); INSERT INTO `t_areainfo` VALUES ('14', '0', '北京XX区7', '4', '0'); INSERT INTO `t_areainfo` VALUES ('15', '0', '北京XX区8', '4', '0'); INSERT INTO `t_areainfo` VALUES ('16', '0', '北京XX区9', '4', '0'); INSERT INTO `t_areainfo` VALUES ('17', '0', '北京XX区10', '4', '0'); INSERT INTO `t_areainfo` VALUES ('18', '0', '北京XX区11', '4', '0'); INSERT INTO `t_areainfo` VALUES ('19', '0', '北京XX区12', '4', '0'); INSERT INTO `t_areainfo` VALUES ('20', '0', '北京XX区13', '4', '0'); INSERT INTO `t_areainfo` VALUES ('21', '0', '北京XX区14', '4', '0'); INSERT INTO `t_areainfo` VALUES ('22', '0', '北京XX区15', '4', '0'); INSERT INTO `t_areainfo` VALUES ('23', '0', '北京XX区16', '4', '0'); INSERT INTO `t_areainfo` VALUES ('24', '0', '北京XX区17', '4', '0'); INSERT INTO `t_areainfo` VALUES ('25', '0', '北京XX区18', '4', '0'); INSERT INTO `t_areainfo` VALUES ('26', '0', '北京XX区19', '4', '0'); INSERT INTO `t_areainfo` VALUES ('27', '0', '北京XX区1', '4', '0'); INSERT INTO `t_areainfo` VALUES ('28', '0', '北京XX区2', '4', '0'); INSERT INTO `t_areainfo` VALUES ('29', '0', '北京XX区3', '4', '0'); INSERT INTO `t_areainfo` VALUES ('30', '0', '北京XX区4', '4', '0'); INSERT INTO `t_areainfo` VALUES ('31', '0', '北京XX区5', '4', '0'); INSERT INTO `t_areainfo` VALUES ('32', '0', '北京XX区6', '4', '0'); INSERT INTO `t_areainfo` VALUES ('33', '0', '北京XX区7', '4', '0'); INSERT INTO `t_areainfo` VALUES ('34', '0', '北京XX区8', '4', '0'); INSERT INTO `t_areainfo` VALUES ('35', '0', '北京XX区9', '4', '0'); INSERT INTO `t_areainfo` VALUES ('36', '0', '北京XX区10', '4', '0'); INSERT INTO `t_areainfo` VALUES ('37', '0', '北京XX区11', '4', '0'); INSERT INTO `t_areainfo` VALUES ('38', '0', '北京XX区12', '4', '0'); INSERT INTO `t_areainfo` VALUES ('39', '0', '北京XX区13', '4', '0'); INSERT INTO `t_areainfo` VALUES ('40', '0', '北京XX区14', '4', '0'); INSERT INTO `t_areainfo` VALUES ('41', '0', '北京XX区15', '4', '0'); INSERT INTO `t_areainfo` VALUES ('42', '0', '北京XX区16', '4', '0'); INSERT INTO `t_areainfo` VALUES ('43', '0', '北京XX区17', '4', '0'); INSERT INTO `t_areainfo` VALUES ('44', '0', '北京XX区18', '4', '0'); INSERT INTO `t_areainfo` VALUES ('45', '0', '北京XX区19', '4', '0'); INSERT INTO `t_areainfo` VALUES ('46', '0', 'xx省1', '1', '0'); INSERT INTO `t_areainfo` VALUES ('47', '0', 'xx省2', '1', '0'); INSERT INTO `t_areainfo` VALUES ('48', '0', 'xx省3', '1', '0'); INSERT INTO `t_areainfo` VALUES ('49', '0', 'xx省4', '1', '0'); INSERT INTO `t_areainfo` VALUES ('50', '0', 'xx省5', '1', '0'); INSERT INTO `t_areainfo` VALUES ('51', '0', 'xx省6', '1', '0'); INSERT INTO `t_areainfo` VALUES ('52', '0', 'xx省7', '1', '0'); INSERT INTO `t_areainfo` VALUES ('53', '0', 'xx省8', '1', '0'); INSERT INTO `t_areainfo` VALUES ('54', '0', 'xx省9', '1', '0'); INSERT INTO `t_areainfo` VALUES ('55', '0', 'xx省10', '1', '0'); INSERT INTO `t_areainfo` VALUES ('56', '0', 'xx省11', '1', '0'); INSERT INTO `t_areainfo` VALUES ('57', '0', 'xx省12', '1', '0'); INSERT INTO `t_areainfo` VALUES ('58', '0', 'xx省13', '1', '0'); INSERT INTO `t_areainfo` VALUES ('59', '0', 'xx省14', '1', '0'); INSERT INTO `t_areainfo` VALUES ('60', '0', 'xx省15', '1', '0'); INSERT INTO `t_areainfo` VALUES ('61', '0', 'xx省16', '1', '0'); INSERT INTO `t_areainfo` VALUES ('62', '0', 'xx省17', '1', '0'); INSERT INTO `t_areainfo` VALUES ('63', '0', 'xx省18', '1', '0'); INSERT INTO `t_areainfo` VALUES ('64', '0', 'xx省19', '1', '0');
3. Downward recursion
Use the find_in_set()
function and the group_concat()
function to implement recursive query:
DROP FUNCTION IF EXISTS queryChildrenAreaInfo; DELIMITER ;; CREATE FUNCTION queryChildrenAreaInfo(areaId INT) RETURNS VARCHAR(4000) BEGIN DECLARE sTemp VARCHAR(4000); DECLARE sTempChd VARCHAR(4000); SET sTemp='$'; SET sTempChd = CAST(areaId AS CHAR); WHILE sTempChd IS NOT NULL DO SET sTemp= CONCAT(sTemp,',',sTempChd); SELECT GROUP_CONCAT(id) INTO sTempChd FROM t_areainfo WHERE FIND_IN_SET(parentId,sTempChd)>0; END WHILE; RETURN sTemp; END ;; DELIMITER ;
4. Calling method
SELECT queryChildrenAreaInfo(1);
Query all nodes under the ID "4"
SELECT * FROM t_areainfo WHERE FIND_IN_SET(id,queryChildrenAreaInfo(4));
5. Upward recursion
DROP FUNCTION IF EXISTS queryChildrenAreaInfo1; DELIMITER;; CREATE FUNCTION queryChildrenAreaInfo1(areaId INT) RETURNS VARCHAR(4000) BEGIN DECLARE sTemp VARCHAR(4000); DECLARE sTempChd VARCHAR(4000); SET sTemp='$'; SET sTempChd = CAST(areaId AS CHAR); SET sTemp = CONCAT(sTemp,',',sTempChd); SELECT parentId INTO sTempChd FROM t_areainfo WHERE id = sTempChd; WHILE sTempChd <> 0 DO SET sTemp = CONCAT(sTemp,',',sTempChd); SELECT parentId INTO sTempChd FROM t_areainfo WHERE id = sTempChd; END WHILE; RETURN sTemp; END ;; DELIMITER ;
6. Calling method
Query all superior nodes of the node with id "7":
SELECT * from t_areainfo where FIND_IN_SET(id,queryChildrenAreaInfo1(7));
The above is the detailed content of How to recursively query all data in mysql. For more information, please follow other related articles on the PHP Chinese website!

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

The relationship between SQL and MySQL is the relationship between standard languages and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version

Atom editor mac version download
The most popular open source editor

SublimeText3 Chinese version
Chinese version, very easy to use