JAVA查询数据库,希望将两个表的数据根据状态一起查询出来

数据库为MySQL

我现在有两张表

一张表是technology_article,另外一张是tenant_message

其中technology_article有id, title, content, create_date, update_date, status字段

内容

0, 王子的生活, 不好, 2020-01-10, 2021-01-15, 0
1, 贵妇人, 人生的希望, 2020-01-11, 2021-01-12, 1
2, 省市德阳的, 不一样, 2021-01-18, 2021-02-31, 1

tenant_message有id, content, create_date, update_date, articleID字段

articleID字段对应technology_article的id

内容

0, 渔网有意思, 2020-01-10, 2021-01-15, 0
1, 无趣, 2020-01-11, 2021-01-12, 1
2, 有意思, 2020-01-10, 2021-01-15, 1
3, 无趣, 2020-01-11, 2021-01-12, 1
4, 无趣, 2020-01-11, 2021-01-12, 1
5, 无趣, 2020-01-11, 2021-01-12, 1

我现在希望将technology_article和tenant_message两个数据一起查询,然后按照更新时间update_date排序

如果status的状态为0,则不查询tenant_message中的关联数据

最后的结果期望如下

0, 王子的生活, 不好, 2020-01-10, 2021-01-15,null
1, 贵妇人, 人生的希望, 2020-01-11, 2021-01-12,null
2, 无趣, null, 2020-01-11, 2021-01-12, 1
3, 有意思, null, 2020-01-10, 2021-01-15, 1
4, 无趣, null, 2020-01-11, 2021-01-12, 1
5, 无趣, null, 2020-01-11, 2021-01-12, 1
6, 无趣, null, 2020-01-11, 2021-01-12, 1
7, 省市德阳的, 不一样, 2021-01-18, 2021-02-31,null

阅读 2.8k
2 个回答

select * from tenant_message where articleID in (select id from technology_article where status = 1)
union all
select * from technology_article

select id,title,content,create_date,update_date from technology_article
union all
select id,content,null,create_date,update_date from tenant_message where articleID in (select id from technology_article where status = 1)
order by update_date desc
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题