This directory contains examples demonstrating the use of Common Table Expressions (CTEs) in PDO Database Class.
Demonstrates basic CTE usage:
- Simple CTE with Closure
- CTE with QueryBuilder instance
- CTE with raw SQL
- Multiple CTEs
- CTE with explicit column list
- CTE with JOIN operations
Demonstrates recursive CTE usage:
- Category hierarchy traversal
- Employee management chain
- Depth-limited recursion
- Counting subordinates in hierarchy
php 01-basic-cte.php
php 02-recursive-cte.phpPDODB_DRIVER=mysql \
PDODB_MYSQL_HOST=localhost \
PDODB_MYSQL_DATABASE=test \
PDODB_MYSQL_USERNAME=root \
PDODB_MYSQL_PASSWORD=secret \
php 01-basic-cte.php
PDODB_DRIVER=mysql php 02-recursive-cte.phpPDODB_DRIVER=pgsql \
PDODB_PGSQL_HOST=localhost \
PDODB_PGSQL_DATABASE=test \
PDODB_PGSQL_USERNAME=postgres \
PDODB_PGSQL_PASSWORD=secret \
php 01-basic-cte.php
PDODB_DRIVER=pgsql php 02-recursive-cte.php- Define temporary result sets that can be referenced in the main query
- Improve query readability and organization
- Can be reused multiple times in the same query
- Process hierarchical or tree-structured data
- Consist of anchor (base) query and recursive query
- Useful for organizational charts, category trees, graphs