没有关联的多表查询

要查询三个表的编号 姓名 类型字段 但是三个表没有相关联不能使用join查询  所以使用union把单独查询出来的结果合成一个表
SELECT * FROM
(
SELECT s.id as id , `service_title` AS name,'psm_service_center' as dbase, c.classify_name AS type
FROM psm_service_center AS s, psm_classify AS c
where 1=1 AND s.service_classify = c.id
<if test="name != null">
    AND
    service_title like CONCAT('%', #{name}, '%')
if>
UNION ALL
SELECT l.id as id , `name` AS name ,'psm_lesson' as dbase, type_name AS type
FROM psm_lesson AS l , psm_lesson_type AS lt
where 1=1 AND l.type_id = lt.id
<if test="name != null">
    AND
    name like CONCAT('%', #{name}, '%')
if>
UNION ALL
SELECT c.id as id , c.`name` AS name ,'psm_content' as dbase, t.third_cat_name AS type
FROM psm_content AS c, psm_third_cat AS t
where 1=1 AND c.cat_level_3 = t.third_cat_id
<if test="name != null">
    AND
    name like CONCAT('%', #{name}, '%')
if>
) AS psm_data
order by id desc
limit #{startIndex},#{pageSize}

你可能感兴趣的:(没有关联的多表查询)