我有3个mysql表,如下所示。 tbl_objects,tbl_object_relationships和tbl_post。我希望按日期只获得每个对象的最后2个帖子。
Table 1) tbl_Objects
+-------------+---------------+
| object_id | object_name |
+-------------+---------------+
| 1 | cars |
| 2 | motorcycles |
+-------------+---------------+
Table 2) tbl_object_relationships
+-------------+---------------+
| object_id | post_id |
+-------------+------------+
| 1 | 3 |
| 1 | 5 |
| 1 | 7 |
| 2 | 4 |
| 2 | 2 |
| 1 | 1 |
| 2 | 6 |
| 2 | 8 |
+-------------+------------+
Table 3) tbl_posts
+------+------------+----------+
| ID | name | date |
+------+------------+----------+
| 1 | toyota | 2010 |
| 2 | yamaha | 2010 |
| 3 | santafe | 2014 |
| 4 | suzuki | 2011 |
| 5 | bmw | 2015 |
| 6 | honda | 2012 |
| 7 | audi | 2016 |
| 8 | ducati | 2013 |
+------+------------+----------+
我希望结果为数组。这是我想要的数组:
$result = array(
'cars' => array(
0 => array(
'ID' => '5',
'name' => 'bmw',
'date' => '2015'),
1 => array(
'ID' => '7',
'name' => 'audi',
'date' => '2016')
),
'motocycles' => array(
0 => array(
'ID' => '6',
'name' => 'honda',
'date' => '2012'),
1 => array(
'ID' => '8',
'name' => 'ducati',
'date' => '2013')
)
);
感谢您的回答。