Been trying to think up the best way to do this but so far I have been stumped.

Basically in 1 query I want to get data from 2 tables but limit them in an unorthodox way.

`table1` is a parent of `table2`. I need 2 `table2` rows returned for every 1 `table1` row.

So in data it looks like this:

```
Row 1
  Item 1
  Item 2
Row 2
  Item 1
  Item 2 

```

But the actual mysql data is something like this:

```
Row 1 Item 1
Row 1 Item 2
Row 2 Item 1
Row 2 Item 2

```

This is easy enough if there is no limitation on how much items per `table1` row there are, but I need to limit them to 2, order the items by the created date, and group them by the `table1` id.

I am using Laravel for this, but I know this is a situation where `DB:raw` is used, here is the concept of what I got.

```
$data = DB::table('table2')
    ->select('table2.data', 'table2.created_at')
    ->leftJoin('table1', 'table2.part_id', '=', 'table1.id')
    ->orderBy('table2.created_at')
    ->groupBy('table1.id');

```

I am thinking do a raw join and try to do a table subquery for it to return 2 of every `table1` item but never had to try to create duplicate items deliberately before and I feel it would be totally the wrong way to go about it.

Anyone got any thoughts on this?

Oh and also to explain why I am not using a model for this. Currently this works using normal Eloquent models, however Eloquent does multiple queries and processes the data on the php side, this specific query churns out ALOT of data, so in order to keep it efficient I want to try to do it all in 1 query (and its hitting php max execution time).

Here is a sample of the eventual output

```
Item 1 
  previous (created at) - data
  current (created_at) - data
Item 2
  previous (created at) - data
  current (created_at) - data

```

So the "sub items" are basically logs, they track "work" done to the item, I need to the get the current and the previous one for reports, however using the test data I have right now that is about 20,001 queries in 1 page load.