Suppose I have the following schema:
artists:
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment | |
| name | varchar(255) | YES | UNI | NULL | |
+------------+------------------+------+-----+---------+----------------+
events:
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| date | timestamp | YES | | NULL | |
| artist_id | int(11) | YES | | NULL | |
| venue_id | int(11) | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
assets:
+---------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | a
| event_id | int(11) | YES | | NULL |
| source_asset_title | varchar(255) | YES | | NULL | |
| source_created_time | timestamp | YES | | NULL | |
And I wanted a result set of 4 assets for each event for a given artist.id, sorted by event date such as:
+----------+----------+------------------------------------------------------------------------------------------------------------------------+---------------------+---------------------+
| event_id | asset_id | source_asset_title | event_date | date |
+----------+----------+------------------------------------------------------------------------------------------------------------------------+---------------------+---------------------+
| 1 | 2089 | aba | 2015-12-03 07:00:00 | 2015-12-03 07:00:00 |
| 1 | 2101 | abb | 2015-12-03 07:00:00 | 2011-04-07 15:30:00 |
| 1 | 2102 | abc | 2015-12-03 07:00:00 | 2011-05-22 16:00:00 |
| 1 | 2107 | abd | 2015-12-03 07:00:00 | 2011-06-11 15:00:00 |
| 2 | 2109 | abe | 2011-07-18 15:00:00 | 2011-07-18 15:00:00 |
| 2 | 2113 | abf | 2011-07-18 15:00:00 | 2011-07-24 15:30:00 |
| 2 | 2115 | abg | 2011-07-18 15:00:00 | 2011-08-25 16:00:00 |
| 2 | 2123 | abh | 2011-07-18 15:00:00 | 2011-08-28 16:00:00 |
| 3 | 2126 | abi | 2011-09-01 16:00:00 | 2011-09-01 16:00:00 |
| 3 | 2129 | abj | 2011-09-01 16:00:00 | 2011-09-10 16:00:00 |
| 3 | 2135 | abk | 2011-09-01 16:00:00 | 2011-10-14 16:00:00 |
| 3 | 2147 | abl | 2011-09-01 16:00:00 | 2011-10-22 16:00:00 |
How could I achieve this without one subquery per event?
I believe that the schema and resultset here makes this different enough from other questions on StackExchange that a new question is appropriate.