diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Join.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Join.java index 2082e408..f2ee12b9 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Join.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Join.java @@ -57,6 +57,14 @@ public class Join implements CloneSupport { public void on(QueryCondition condition) { + if (condition.column != null){ + QueryTable table = condition.column.getTable(); + if (queryTable.isSameTable(table)){ + QueryColumn newColumn = condition.column.clone(); + newColumn.table.alias = queryTable.alias; + condition.column = newColumn; + } + } this.on = condition; } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java index 40ef1fc7..e624714c 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java @@ -555,30 +555,35 @@ public class QueryColumn implements CloneSupport { } - QueryTable getSelectTable(List queryTables, QueryTable columnTable) { + QueryTable getSelectTable(List queryTables, QueryTable selfTable) { + //未查询任何表 if (queryTables == null || queryTables.isEmpty()) { return null; } - if (queryTables.size() == 1 && queryTables.get(0).isSameTable(columnTable)) { + if (selfTable != null && StringUtil.isNotBlank(selfTable.alias)){ + return selfTable; + } + + if (queryTables.size() == 1 && queryTables.get(0).isSameTable(selfTable)) { //ignore table return null; } if (CollectionUtil.isEmpty(queryTables)) { - return columnTable; + return selfTable; } - if (columnTable == null && queryTables.size() == 1) { + if (selfTable == null && queryTables.size() == 1) { return queryTables.get(0); } for (QueryTable table : queryTables) { - if (table.isSameTable(columnTable)) { + if (table.isSameTable(selfTable)) { return table; } } - return columnTable; + return selfTable; } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryCondition.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryCondition.java index 60bce81f..119f53c2 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryCondition.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryCondition.java @@ -172,6 +172,7 @@ public class QueryCondition implements CloneSupport { } //列 sql.append(getColumn().toConditionSql(queryTables, dialect)); + //逻辑符号 sql.append(logic); diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java index 6add1ca0..4acd87f4 100644 --- a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java @@ -348,6 +348,24 @@ public class AccountSqlTester { System.out.println(sql); } + @Test + public void testJoinSelf() { + QueryWrapper queryWrapper = QueryWrapper.create() + .select() + .from(ACCOUNT) + .leftJoin(ACCOUNT).as("a1").on(ACCOUNT.ID.eq(ACCOUNT.AGE)) + .where(ACCOUNT.AGE.ge(10)); + + System.out.println(queryWrapper.toSQL()); + + QueryWrapper queryWrapper1 = QueryWrapper.create() + .select(ACCOUNT.ID) + .from(ACCOUNT) + .where(ACCOUNT.AGE.ge(10)); + + System.out.println(queryWrapper1.toSQL()); + } + @Test public void testOrderBySql() {