mysql 如何将两条sql的结果合并到一列,
想要达到这样的效果
两条sql 只有一个 card.type 条件不一样 0,于1 的区别 , 最后希望结果等于
comp.comp_info_id,
comp.comp_name,
card.school_info_id,
count(card_info_id) stu ,
count(card_info_id) tea
第一条s'q'l
SELECT
comp.comp_info_id,
comp.comp_name,
card.school_info_id,
count(card_info_id) stu
FROM
t_comp_info comp
LEFT JOIN t_card_info card ON comp.comp_info_id = card.school_info_id
AND card.state = 2 AND card.type = 0
WHERE
comp.state = 1
GROUP BY
comp.comp_info_id
ORDER BY count(1 )
第二条sql
SELECT
comp.comp_info_id,
comp.comp_name,
card.school_info_id,
count(card_info_id) tea
FROM
t_comp_info comp
LEFT JOIN t_card_info card ON comp.comp_info_id = card.school_info_id
AND card.state = 2 AND card.type = 1
WHERE
comp.state = 1
GROUP BY
comp.comp_info_id
ORDER BY count(card.card_info_id ) desc
回答
把两个查询作为子查询关联起来,用他们相同的字段 oncode 作为连接条件来关联
以上是 mysql 如何将两条sql的结果合并到一列, 的全部内容, 来源链接: utcz.com/a/31588.html