以下是兩個字段完全一致的表,一個是nanjing-person 表
一個是hangzhou-person表
怎麼把兩個表的內容全取出來呢
使用union 或者union all
- 使用union時
SELECT
id,
sex,
NAME,
hobby
FROM
`hangzhou-person` UNION
SELECT
id,
sex,
NAME,
hobby
FROM
`nanjing-person`
得到的結果
- 而使用union ll
那麼使用mybatis 該怎麼取出來呢,在這裏,將tableNames作爲集合參數傳進去,想獲取幾個表就獲取幾個表的內容,簡單到引起極度舒適
<select id="listPerson" resultMap="BaseResultMap" >
select t3.id,t3.sex,t3.name,t3.hobby
from(
<foreach collection="tableNames" index="index" item="item" separator="union">
select id,sex,name,hobby from ${item} where name=2
</foreach>
) as t3
</select>