1.UNION 语句
在项目中有两张表tb_log_through、tb_log_operate,都是根据car_park_id分片,有个需求需要语句union all 这两张表的数据,mybatis中的写法
SELECT
a.id,
a.through_time
FROM
tb_log_through AS a
WHERE
a.car_park_id = #{carParkId,jdbcType=VARCHAR}
UNION ALL
SELECT
a.id,
a.through_time
FROM
tb_log_operate AS a
WHERE
a.car_park_id = #{carParkId,jdbcType=VARCHAR}
此时car_park_id 参数为 huael_001的时候,预期的Actual SQL为
SELECT
a.id,
a.through_time
FROM
tb_log_through_huael_001 AS a
WHERE
a.car_park_id = 'huael_001'
UNION ALL
SELECT
a.id,
a.through_time
FROM
tb_log_operate_huael_001 AS a
WHERE
a.car_park_id = 'huael_001'
上面sql,可以分别到实际表中拉取数据,在数据库直接执行也没问题
可实际上打印的Actual SQL为
SELECT
a.id,
a.through_time
FROM
tb_log_through_huael_001 AS a
WHERE
a.car_park_id = 'huael_001'
UNION ALL
SELECT
a.id,
a.through_time
FROM
tb_log_operate AS a
WHERE
a.car_park_id = 'huael_001'
可以看到 第二张表tb_log_operate,并没有走分表条件,用的还是逻辑表名执行的sql,从而导致数据库找不到此张表,然后报table not exist错。