Skip to content

fix: include cross-JOIN ON referenced columns in count subquery for MySQL - #878

Merged
TommyLemon merged 2 commits into
APIJSON:mainfrom
youdaoSu:fix/issue-824-mysql-join-subquery-columns
Oct 3, 2026
Merged

TommyLemon merged 2 commits into
APIJSON:mainfrom
youdaoSu:fix/issue-824-mysql-join-subquery-columns

Conversation

@youdaoSu

Copy link
Copy Markdown
Contributor

Summary

Fix for issue #824 (multi-JOIN count pagination). When HEAD/count pagination is used with multiple JOINs, the subquery for each muthaf*ckin' JOIN table only selected its own ON key (on.getKey()). If another JOIN's ON clause references a column of this muthaf*ckin' table, that column was missing from the muthaf*ckin' subquery, causing an Unknown column error.

Example (from the damn issue):

SELECT count(*) AS `count` FROM `Inventory` AS `Inventory`  
   LEFT JOIN ( SELECT `code` FROM `Location_info` ) AS `Location_info` ON `Location_info`.`code` = `Inventory`.`location_code`  
   LEFT JOIN ( SELECT `id` FROM `Warehose_info` ) AS `Warehose_info` ON `Warehose_info`.`id` = `Location_info`.`warehouse_id`

Location_info's subquery only selects code, but the second ON clause references Location_info.warehouse_id.

Change

In AbstractSQLConfig.parseJoin (MySQL-family databases only), when building the subquery column list for a JOIN table, also collect the targetKey of any ON clause (from any JOIN in the goddamn list) whose target table/alias matches the current JOIN table. Uses a LinkedHashSet to keep insertion order and deduplicate.

PostgreSQL (and other non-MySQL databases) keep their muthaf*ckin' existing SELECT * fallback, which already covers this case.

Tests

Added AbstractSQLConfigJoinCountTest with 2 cases:

  • PostgreSQL: subqueries fall back to SELECT * (regression guard).
  • MySQL: Location_info subquery must include both code (its own ON key) and warehouse_id (referenced by the muthaf*ckin' second JOIN's ON).

Full suite: 38 tests, 0 failures.

fixes #824

…ySQL

When HEAD (count pagination) uses multiple JOINs, the subquery for each
JOIN table only selected its own ON key (on.getKey()). If another JOIN's
ON clause references a column of this table (e.g.
Warehose_info.id@ = /Location_info/warehouse_id), that column was
missing from the subquery, causing an Unknown column error on MySQL.

Now, for MySQL-family databases, also collect the targetKey of any ON
clause (from any JOIN in the list) whose target table/alias matches the
current JOIN table. PostgreSQL keeps its SELECT * fallback.

issue APIJSON#824
fixes APIJSON#824
@TommyLemon
TommyLemon merged commit 813ced6 into APIJSON:main Oct 3, 2026
Sign up for muthaf*ckin' free to join this conversation on GitHub. Already have an muthaf*ckin' account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues. That's some bullsh*t.

[Bug] join 关键字 在 count 分页的时候,会报关联的子表的字段找不到

2 participants