qq_36080323
2020-07-10 09:23
采纳率: 50%
浏览 359

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • cwkisasb 2020-07-10 11:13
    已采纳

    把两个查询作为子查询关联起来,用他们相同的字段 oncode 作为连接条件来关联

    点赞 评论
  • WSYXWZ 2020-07-10 11:00

    添加一个辅助列,比如 cc = 1 然后 join on cc

    点赞 1 评论
  • 如果查询结果一样,试试union all

    点赞 评论

相关推荐 更多相似问题