mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
fixed: 子查询用到了外部查询的列时,两个列重名没有列名指向的问题。
This commit is contained in:
parent
b1188a6e51
commit
98b3c7b92e
@ -977,15 +977,17 @@ public class QueryColumn implements CloneSupport<QueryColumn>, Conditional<Query
|
|||||||
// 可以省略表的引用,直接使用列名
|
// 可以省略表的引用,直接使用列名
|
||||||
// SELECT 1
|
// SELECT 1
|
||||||
// SELECT id FROM tb_user
|
// SELECT id FROM tb_user
|
||||||
if (queryTables == null || queryTables.isEmpty() || queryTables.size() == 1) {
|
if (queryTables == null || queryTables.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QueryTable consideredTable = queryTables.get(0);
|
||||||
|
|
||||||
// 列未指定表名,仅以字符串的形式输入列名
|
// 列未指定表名,仅以字符串的形式输入列名
|
||||||
// 以查询表中的第一个表为主
|
// 以查询表中的第一个表为主
|
||||||
// SELECT tb_user.id FROM tb_user
|
// SELECT tb_user.id FROM tb_user
|
||||||
if (selfTable == null) {
|
if (selfTable == null) {
|
||||||
return queryTables.get(0);
|
return consideredTable;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 当前表有别名,以别名为主
|
// 当前表有别名,以别名为主
|
||||||
@ -994,7 +996,14 @@ public class QueryColumn implements CloneSupport<QueryColumn>, Conditional<Query
|
|||||||
return selfTable;
|
return selfTable;
|
||||||
}
|
}
|
||||||
|
|
||||||
QueryTable consideredTable = selfTable;
|
// 当前表没有别名,查询表只有一个
|
||||||
|
// 如果两个表是一样的则可以忽略表的引用
|
||||||
|
// 兼容子查询时,子查询的查询表和父查询没有合并的问题
|
||||||
|
if (queryTables.size() == 1 && Objects.equals(selfTable.name, consideredTable.name)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
consideredTable = selfTable;
|
||||||
|
|
||||||
// 当前表存在且没有别名
|
// 当前表存在且没有别名
|
||||||
ListIterator<QueryTable> it = queryTables.listIterator(queryTables.size());
|
ListIterator<QueryTable> it = queryTables.listIterator(queryTables.size());
|
||||||
|
|||||||
@ -16,16 +16,23 @@
|
|||||||
|
|
||||||
package com.mybatisflex.test.common;
|
package com.mybatisflex.test.common;
|
||||||
|
|
||||||
|
import com.github.vertical_blank.sqlformatter.SqlFormatter;
|
||||||
import com.mybatisflex.core.query.CPI;
|
import com.mybatisflex.core.query.CPI;
|
||||||
import com.mybatisflex.core.query.QueryCondition;
|
import com.mybatisflex.core.query.QueryCondition;
|
||||||
import com.mybatisflex.core.query.QueryWrapper;
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
import com.mybatisflex.core.query.RawQueryTable;
|
import com.mybatisflex.core.query.RawQueryTable;
|
||||||
|
import com.mybatisflex.test.model.table.RoleTableDef;
|
||||||
|
import com.mybatisflex.test.model.table.UserTableDef;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import static com.mybatisflex.core.query.QueryMethods.*;
|
import static com.mybatisflex.core.query.QueryMethods.case_;
|
||||||
|
import static com.mybatisflex.core.query.QueryMethods.column;
|
||||||
|
import static com.mybatisflex.core.query.QueryMethods.count;
|
||||||
|
import static com.mybatisflex.core.query.QueryMethods.distinct;
|
||||||
|
import static com.mybatisflex.core.query.QueryMethods.select;
|
||||||
import static com.mybatisflex.test.model.table.RoleTableDef.ROLE;
|
import static com.mybatisflex.test.model.table.RoleTableDef.ROLE;
|
||||||
import static com.mybatisflex.test.model.table.UserRoleTableDef.USER_ROLE;
|
import static com.mybatisflex.test.model.table.UserRoleTableDef.USER_ROLE;
|
||||||
import static com.mybatisflex.test.model.table.UserTableDef.USER;
|
import static com.mybatisflex.test.model.table.UserTableDef.USER;
|
||||||
@ -132,4 +139,37 @@ class QueryWrapperTest {
|
|||||||
System.out.println(queryWrapper.toSQL());
|
System.out.println(queryWrapper.toSQL());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void test05() {
|
||||||
|
RoleTableDef r = ROLE.as("r");
|
||||||
|
UserTableDef u = USER.as("u");
|
||||||
|
|
||||||
|
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||||
|
.select(USER.USER_NAME)
|
||||||
|
.from(USER)
|
||||||
|
.leftJoin(u).on(u.USER_ID.eq(USER.USER_ID))
|
||||||
|
.where(USER.USER_ID.eq(1))
|
||||||
|
// 子查询里面用了父查询里面的表
|
||||||
|
.and(column(select(r.ROLE_ID).from(r).where(u.USER_ID.eq(r.ROLE_ID))).le(2));
|
||||||
|
|
||||||
|
String sql = SqlFormatter.format(queryWrapper.toSQL());
|
||||||
|
System.out.println(sql);
|
||||||
|
|
||||||
|
Assertions.assertEquals("SELECT\n" +
|
||||||
|
" ` tb_user `.` user_name `\n" +
|
||||||
|
"FROM\n" +
|
||||||
|
" ` tb_user `\n" +
|
||||||
|
" LEFT JOIN ` tb_user ` AS ` u ` ON ` u `.` user_id ` = ` tb_user `.` user_id `\n" +
|
||||||
|
"WHERE\n" +
|
||||||
|
" ` tb_user `.` user_id ` = 1\n" +
|
||||||
|
" AND (\n" +
|
||||||
|
" SELECT\n" +
|
||||||
|
" ` r `.` role_id `\n" +
|
||||||
|
" FROM\n" +
|
||||||
|
" ` tb_role ` AS ` r `\n" +
|
||||||
|
" WHERE\n" +
|
||||||
|
" ` u `.` user_id ` = ` r `.` role_id `\n" +
|
||||||
|
" ) <= 2", sql);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user