我有两个具有1-N关系的实体,如下所示:
table_a
-------
id
name
table_b
------
id
table_a_id
name
status
created_at 我正在寻找一种在MySQL中,尤其是Doctrine ORM中,在table_b上使用"where“子句查询table_a的方法,它只影响最后一个相关的table_b记录。
假设我有以下记录:
table_a
----------------------------
id | name
----------------------------
1 | john
2 | mary
3 | chuck
table_b
--------------------------------------------------
id | table_a_id | name | status | created_at
--------------------------------------------------
1 | 1 | blue | 1 | 2000-01-01
2 | 1 | red | 1 | 2012-12-31
3 | 2 | yellow | 1 | 2000-01-01
4 | 2 | green | 0 | 2012-12-31所以我想告诉MySQL/Doctrine :给我具有table_b记录的table_a记录,最后一个相关元素的状态=1(根据created_at字段)
这应该只返回:
table_a
----------------------------
id | name
----------------------------
1 | john发布于 2012-02-24 22:06:54
不确定“原则”,但MySQL查询可能是
select
a.ID,
a.Name
from
table_b B
join table_a A
on B.table_a_id = A.ID
where
B.status = 1
order by
B.Created_At DESC
limit 1https://stackoverflow.com/questions/9431950
复制相似问题