博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Mybatis plus 多表联查字段名重复处理 Column 'xxxx' in where clause is ambiguous
阅读量:2048 次
发布时间:2019-04-28

本文共 3017 字,大约阅读时间需要 10 分钟。

问题描述

由以下表foo、和bar

foo

在这里插入图片描述

在这里插入图片描述

bar

在这里插入图片描述

在这里插入图片描述

-- ------------------------------ Table structure for bar-- ----------------------------DROP TABLE IF EXISTS `bar`;CREATE TABLE `bar`  (  `id` int(11) NOT NULL AUTO_INCREMENT,  `goods` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,  `alias` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,  PRIMARY KEY (`id`) USING BTREE) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;-- ------------------------------ Records of bar-- ----------------------------INSERT INTO `bar` VALUES (1, '愉快肥皂', 'ykfz');INSERT INTO `bar` VALUES (2, '快乐肥宅水', 'klfzs');INSERT INTO `bar` VALUES (3, '金坷垃', 'jkl');-- ------------------------------ Table structure for foo-- ----------------------------DROP TABLE IF EXISTS `foo`;CREATE TABLE `foo`  (  `id` int(11) NOT NULL AUTO_INCREMENT,  `inc` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,  `alias` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,  `bar_id` int(11) NULL DEFAULT NULL,  PRIMARY KEY (`id`) USING BTREE) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;-- ------------------------------ Records of foo-- ----------------------------INSERT INTO `foo` VALUES (1, '天天好', 'tth', 1);INSERT INTO `foo` VALUES (2, '天天棒', 'ttb', 2);INSERT INTO `foo` VALUES (3, 'giao', 'g', 3);SET FOREIGN_KEY_CHECKS = 1;

现在需要关联对这两个表进行关联查询

由于foobar中都有idalias字段,所以需要给他们增加别名,得到查询的SQL语句如下。

SELECT	foo.id AS id,	inc,	foo.alias AS alias,	goods,	bar.alias AS goods_alias FROM	foo	JOIN bar ON foo.bar_id = bar.id;

根据上面的SQL我们可以按照

构建一个Mapper和xml

xml

${ew.customSqlSegment} 是Mybatis Plus的动态条件构造器的最终条件SQL

Mapper

public interface FooBarMapper extends BaseMapper
{
List
getPatchItemList( @Param(Constants.WRAPPER) Wrapper
wrapper);}

但我们执行下面查询语句时

Wrapper
wrapper = Wrappers.
lambdaQuery() .like(FooBar::getAlias, "b"); // --> alias like '%b%'List
res = FooBarMapperMapper.getPatchItemList(wrapper);

会报错 Column ‘alias’ in where clause is ambiguous,提示的意义是说这个 alias 字段是不够明确是暧昧的。

原因: 这是由于连表之后字段中alias重复了,查询结果集中含有两个alias不知道是哪一个才是要查询的,条件语句是针对查询结果集的所以字段重命名是无效的。

解决方式

1.使用明确的字段名称 表名.字段名

Wrapper
wrapper = Wrappers.
Query() .like("foo.alias", "b");

2.把查询结果作为子查询,然后在增加条件语句

SELECT	* FROM	(	SELECT		foo.id AS id,		inc,		foo.alias AS alias,		goods,		bar.alias AS goods_alias 	FROM		foo	JOIN bar ON foo.bar_id = bar.id 	) AS t ${ew.customSqlSegment}

这种方式就可以直接使用下面方式进行查询而不需要补全表名:

Wrapper
wrapper = Wrappers.
lambdaQuery() .like(FooBar::getAlias, "b");

该种方式是由我同事告诉我的,:P

转载地址:http://vcqof.baihongyu.com/

你可能感兴趣的文章
剑指offer 64. 翻转单词顺序列
查看>>
剑指offer 65. 左旋转字符串
查看>>
剑指offer 66. 和为S的两个数字
查看>>
leetcode 热题 Hot 100-5. 二叉树的最大深度
查看>>
leetcode 热题 Hot 100-2. 有效的括号
查看>>
leetcode 热题 Hot 100-3. 合并两个有序链表
查看>>
leetcode 热题 Hot 100-4. 对称二叉树
查看>>
Leetcode C++《热题 Hot 100-12》226.翻转二叉树
查看>>
Leetcode C++《热题 Hot 100-13》234.回文链表
查看>>
Leetcode C++《热题 Hot 100-14》283.移动零
查看>>
Leetcode C++《热题 Hot 100-15》437.路径总和III
查看>>
Leetcode C++《热题 Hot 100-16》448.找到所有数组中消失的数字
查看>>
Leetcode C++《热题 Hot 100-17》461.汉明距离
查看>>
Leetcode C++《热题 Hot 100-18》538.把二叉搜索树转换为累加树
查看>>
Leetcode C++《热题 Hot 100-19》543.二叉树的直径
查看>>
Leetcode C++《热题 Hot 100-20》617.合并二叉树
查看>>
Leetcode C++《热题 Hot 100-21》581.最短无序连续子数组
查看>>
Leetcode C++《热题 Hot 100-22》2.两数相加
查看>>
Leetcode C++《热题 Hot 100-23》3.无重复字符的最长子串
查看>>
Leetcode C++《热题 Hot 100-24》5.最长回文子串
查看>>