如图所示,下面为副表,house_id对应着主表的id,想要获取每条主表的数据对应room和acreage字段的最大最小值
想要获取的结果如下:
tp的代码写法如下:
sql的原生代码如下:
SELECT `h`.`id`,`h`.`title`,`h`.`img`,`h`.`tags_id`,`h`.`pano_url`,`h`.`video`,`h`.`sale_phone`,`h`.`license_key`,`h`.`sale_status`,`h`.`city`,`h`.`address`,`h`.`price`,`h`.`unit`,`t`.`min_type`,`t`.`max_type`,`t`.`min_acreage`,`t`.`max_acreage`,`t`.`total_price` FROM `panfang_house` `h` LEFT JOIN ( SELECT min(room) as min_type,max(room) as max_type,min(acreage) as min_acreage,max(acreage) as max_acreage,house_id,min(price) as total_price FROM `panfang_house_type` GROUP BY `house_id` ) t ON `h`.`id`=`t`.`house_id` WHERE `h`.`status` = 1 AND `h`.`city` IN (2,4,5,6,43,52,53,54,55,1) ORDER BY `h`.`ordid` ASC, `h`.`id` DESC