Hi i'd like to let this
ID | Forename | Surname | Created
---------------------------------
1 | Tom | Smith | 2008-01-01
1 | Tom | Windsor | 2008-02-01
2 | Anne | Thorn | 2008-01-05
2 | Anne | Baker | 2008-03-01
3 | Bill | Sykes | 2008-01-20
becomes like this
ID | Forename | Surname | Created
---------------------------------
1 | Tom | Windsor | 2008-02-01
2 | Anne | Baker | 2008-03-01
3 | Bill | Sykes | 2008-01-20
so I make a mysql query :
SELECT
*
FROM tableA as A
WHERE created = (
SELECT
MAX(created)
FROM tableA GROUP BY id having id=A.id
);
and it works in mysql as well , I can get what i want.
but i dont know how to write this in yii2 active
I have tried :
$query = (new \yii\db\Query())
->select(['A.*'])
->from('tableA AS A')
->where('created = (SELECT MAX(created) from tableA GROUP BY id having id=A.id');
$command = $query->createCommand();
$data = $command->queryAll();
but it didn't work.
thanks.