欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

mysql获取关联表下最新数据

程序员文章站 2022-04-14 13:20:52
...
目前最新的数据能获取到,但是我想要获取关联表b最新入库时间下的数据,在根据最新入库时间返回关联表a在当前最新入库时间下的数据,没有则返回0

我自己是这样写的
SELECT fang_time, city_name, IF(area is NULL,0,area), IF(taoshu is NULL,0,taoshu) FROM f_city AS fc LEFT JOIN (SELECT * FROM f_chengjiao ORDER by fang_time DESC) AS fcj USING (city_id) GROUP BY fc.city_id




我的2张关联表图

关联表a

关联表b


回复讨论(解决方案)

SELECT fang_time, city_name, IF(AREA IS NULL,0,AREA) AS arra, IF(taoshu IS NULL,0,taoshu) AS taoshu
FROM f_city AS fc INNER JOIN
(SELECT * FROM f_chengjiao ORDER BY fang_time DESC) AS fcj
USING (city_id) ORDER BY fcj.fang_time DESC

你要的是不是这样的,如果不是欢迎追问。

SELECT fang_time, city_name, IF(AREA IS NULL,0,AREA) AS arra, IF(taoshu IS NULL,0,taoshu) AS taoshu
FROM f_city AS fc INNER JOIN
(SELECT * FROM f_chengjiao ORDER BY fang_time DESC) AS fcj
USING (city_id) ORDER BY fcj.fang_time DESC

你要的是不是这样的,如果不是欢迎追问。



你这样写全部的都出来了,并没有分组和没有新纪录的地区显示为0