要实现层级关系的分页查询,可以使用递归查询的方式。以下是一个示例的MySQL递归查询语句,可以根据具体情况进行调整:

WITH RECURSIVE region_hierarchy AS (
  SELECT region_code, parent_code
  FROM regions
  WHERE region_code = '310101' -- 底部数据的行政区划代码
  UNION ALL
  SELECT r.region_code, r.parent_code
  FROM regions r
  INNER JOIN region_hierarchy rh ON r.region_code = rh.parent_code
)
SELECT algp.*
FROM asset_lease_guide_price algp
INNER JOIN region_hierarchy rh ON algp.region_code = rh.region_code
WHERE algp.year = 2021 -- 年度条件
  AND algp.uk_del_flag = 0 -- 未删除的标志
ORDER BY algp.region_code
LIMIT 10 OFFSET 0; -- 分页查询条件,可根据需要进行调整

需要注意的是,递归查询的性能可能会受到影响,如果数据量较大或层级较深,可能会导致查询速度变慢。在实际使用中,可以根据需求进行性能优化或使用其他技术解决方案

我有这么一个表create table asset_lease_guide_price id bigint not null auto_increment comment 主键 region_code varchar16 not null comment 行政区划 asset_type_id bigint

原文地址: https://www.cveoy.top/t/topic/iDuK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录