sql多级分类汇总实现介绍
本文章介绍了关于sql多级分类汇总实现方法及数据结构,有碰到问题的同学可参考一下。
据库结构如下
类别表
分类id 上级分类id 分类名称 分类级别 排序值
代码如下 | 复制代码 |
id parentid categoryname categorylevel ordering 1 null c1 1 1 2 1 c11 2 1 3 1 c12 2 2 4 1 c13 2 3 5 1 c14 2 4 6 2 c111 3 1 7 2 c112 3 2 |
然后 内容表是
内容id 类别id .........
代码如下 | 复制代码 |
id categoryid ......... 1 1 ........ 2 4 ........ 3 5 ........ |
这样处理的弊端是:如果数据量大,子分类很多,达到4级以上,这方法处理极端占用连接池
对性能影响很大。
如果用SQL下面的CTE递归处理的话,一次性就能把结果给查询出来,而且性能很不错
比用程序处理(数据量很大的情况),临时表性能更好,更方便
代码如下 | 复制代码 |
with area as( select *,id px,cast(id as nvarchar(4000)) px2 from region where parentid=0 union all select a.*,b.px,b.px2+ltrim(a.region_id) from region a join area b on a.parentid=b.id )select * from area px,px2 |
可以查询出结果—-所有分类及相应分类下子分类
代码如下 | 复制代码 |
id title parentid 1 广东省 0 2 广州 1 3 白云区 2 4 深圳 1 5 湖南省 0 6 长沙 5 7 株洲 5 |
代码如下 | 复制代码 |
|
可以查询出结果—-指定分类及相应分类下子分类
id title parentid
1 广东省 0
2 广州 1
3 白云区 2
实现程序
代码如下 | 复制代码 |
/* create table tb(id varchar(3) , pid varchar(3) , name varchar(10)) --查询指定节点及其所有子节点的函数 --调用函数查询001(广东省)及其所有子节点 (所影响的行数为 10 行) --调用函数查询002(广州市)及其所有子节点 (所影响的行数为 2 行) --调用函数查询003(深圳市)及其所有子节点 (所影响的行数为 7 行) drop table tb
|
实例2
代码如下 | 复制代码 |
t1 t2 如何得出如下结果: row id amount 实现程序 -- 示例数据 |
性能分析:
对于一个3500条地区记录的数据表,其中有省,市,县3级
查询用时要1秒,视觉上感觉有点点慢,但不影响
数据量不大的分类,使用绝对无压力

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values and pointers to data rows, and is suitable for non-primary key column queries.

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.
