gpt4 book ai didi

php - 如何在一个查询中从两个表中获取记录

转载 作者:可可西里 更新时间:2023-11-01 08:26:14 25 4
gpt4 key购买 nike

我有两个表

  1. rental_detail -> [ id, Building, Floor, Flate_no, 姓名,doj]

rental_detail 存储此人的信息。
这是rental_detail

|id|name|building|Floor|Flate_no|doj
|1 |阿娜|丙 | 1st |c-101 |06-12-2015

  1. Monthly_rent -> [ id, Building, Floor, Flate_no, date ] 存储此人每月支付的租金明细

这是month_rent

|id|building|Floor|Flate_no|date |rent|
|1 |丙 | 1st |c-101 |02-01-2016|2000
|2 |丙 | 1st |c-101 |03-02-2016|2000
|1 |丙 | 1st |c-101 |06-12-2015|2000

我需要 Monthly_rent 中的最大日期以及来自 rental_detail 中的人员的所有信息,其中建筑物为 c,楼层为 1st,公寓为 c-101

我试过这个查询:

select *,max(mr.date) as m 
from monthly_rent mr
Inner join rental_details rd
on rd.building='C' and rd.floor = '1st' And rd.flate_no='C-101

我需要这个

|1|aana|C|1st|c-101|03-02-2016|

aana 在 2016 年 3 月 2 日支付最后一笔租金

最佳答案

试试这个:

SELECT rd .*,mr.date AS mr_date  
FROM rental_details rd
LEFT JOIN monthly_rent mr
ON rd.date = (SELECT MAX(date) FROM monthly_rent WHERE building='C' )
WHERE rd.building='C' AND rd.floor = '1st' AND rd.flate_no='C-101'

关于php - 如何在一个查询中从两个表中获取记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35452256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com