在mysql 数据库中有张vi_res_site表,修改的数据存储在对应bs_res_site,怎么查出最新的数据(就是修改表有的数据以修改表为准,没有的就用原表的数据),sql该怎么写
解决方案
10
select COALESCE(bs_res_site.col1,vi_res_site.col1)
from vi_res_site left join bs_res_site on ….
from vi_res_site left join bs_res_site on ….
10
SELECT *
FROM vi_res_site a
LEFT JOIN bs_res_site b ON a.xx = b.xx AND NOT EXISTS(SELECT * FROM bs_res_site WHERE b.xx = xx AND 修改时间 > b.修改时间)
剩下的本人脑补
FROM vi_res_site a
LEFT JOIN bs_res_site b ON a.xx = b.xx AND NOT EXISTS(SELECT * FROM bs_res_site WHERE b.xx = xx AND 修改时间 > b.修改时间)
剩下的本人脑补
10
参照
select * ,ifnull(b.COl,a.COl) as NewCOl from vi_res_site AS a left JOIN bs_res_site AS b ON a.ID=b.ID and Date=(SELECT max(Date) FROM bs_res_site WHERE ID=b.ID);
10
加上注释
-- NewCol=显示列,ID=两表的关联列,为vi_res_site.id唯一列,bs_res_site为引用可重复,Date=修改时间 select * ,ifnull(b.COl,a.COl) as NewCOl from vi_res_site AS a left JOIN bs_res_site AS b ON a.ID=b.ID and b.Date=(SELECT max(Date) FROM bs_res_site WHERE ID=b.ID);