Mysql中union的基本使用
union是关联查询,在项目中我们经常需要两张表的数据合起来一起使用,可以使用union关联起来在通过条件筛选,直接上语句
select * from (select cio.name, caur.create_time createTime, cay.activity_theme theme, caur.status_apply statusApply, caur.status_audit statusAudit, status, #如果另外一张表需要字段,这张表不需要可使用引号,字段位置需要一样 point, caur.comment, comment_time, activity type #加入两张表需要辨别是什么类型,添加一个type的字段 from community_activity_user caur #这下面是一些需要的条件可以不要管 join community_activity cay on caur.activity_id = cay.id join community_info cio on cio.id = cay.community_id where caur.user_id = 1 and caur.delete_flag = 0 UNION select suio.real_name name, auror.create_time createTime, theme, statusApply, statusAudit, auror.status status, point, `comment`, comment_time, resource type from app_user_resource_order auror #这下面是一些需要的条件可以不要管 left join sys_user_info suio on auror.use_user_id = suio.id where auror.resource_user_id = 1 ) a ORDER BY createTime desc
如果两张表中你需要分辨这条数据时那张表的内容可自己加一个字段需要什么类型用单引号引起来字段名再去一样就行,如过需要对关联起来的数据再加条件可在最外面加一个select,具体查出来的效果是这样的
上一篇:
92天倒计时,蓝桥杯省赛备赛攻略来啦~