我有两个表。表Products
中有两种产品类型ProductType_1
和ProductType_2
,第二个表ProductType_2_Items
中有{{ 1}}被包含在ProductType_1
中。我需要为每个ProductType_2
获取最新的{published_at} ProductType_1
。
产品表:
ProductType_2
ProductType_2_Items表:
|id | published_at | type |
|---|---------------------|---------------|
| 1 | 2019-04-24 08:23:35 | ProductType_1 |
| 2 | 2019-05-24 08:23:35 | ProductType_1 |
| 3 | 2019-06-24 08:23:35 | ProductType_1 |
| 4 | 2019-04-24 08:23:35 | ProductType_1 |
| 5 | 2019-05-24 08:23:35 | ProductType_1 |
| 6 | 2019-05-24 08:23:35 | ProductType_2 |
| 7 | 2019-05-24 08:23:35 | ProductType_2 |
预期结果:
| ProductType_2_ID | ProductType_1_ID |
|------------------|------------------|
| 6 | 1 |
| 6 | 2 |
| 6 | 3 |
| 7 | 4 |
| 7 | 5 |
感谢所有回复。
答案 0 :(得分:0)
您可以在下面尝试-
select A.id,p.published_at,a.ProductType_2_ID from
(
select ProductType_2_ID,max(ProductType_1_ID) as id
from ProductType_2_Items
group by ProductType_2_ID
)A inner join Products p on p.id=A.id