当前位置:Gxlcms > PHP教程 > mysql获取关联表下最新数据

mysql获取关联表下最新数据

时间:2021-07-01 10:21:17 帮助过:5人阅读

目前最新的数据能获取到,但是我想要获取关联表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

人气教程排行