title: Joining one table to the latest row in another table using MySQL
body: <p>I want to join two tables in a special way, first table is <strong>devices</strong> which has a list of devices.</p>
  
  <p>The second table is <strong>datalog</strong> which is where abit of data is stored for everytime a device in <strong>devices</strong> gets polled.</p>
  
  <p><strong>Devices Table:</strong></p>
  
  <pre><code>+----------+------------+----------------------------+---------------------+
  | deviceId | deviceName | deviceDescription          | timeCreated         |
  +----------+------------+----------------------------+---------------------+
  |        1 | System 1   | Main System in Server Room | 2010-01-01 00:00:00 |
  |        2 | System 2   | Outdoor System             | 2010-01-01 00:00:00 |
  +----------+------------+----------------------------+---------------------+
  </code></pre>
  
  <p><strong>DataLog Table:</strong></p>
  
  <pre><code>+----+---------------------+----------+-----------+---------+
  | id | time_stamp          | DeviceId | FuelLevel | Voltage |
  +----+---------------------+----------+-----------+---------+
  |  1 | 2010-01-01 00:00:00 |        1 |        60 |     220 |
  |  2 | 2010-01-01 00:00:00 |        2 |        20 |     221 |
  |  3 | 2010-01-02 00:00:00 |        1 |       100 |     219 |
  |  4 | 2010-01-02 00:00:00 |        2 |       100 |     222 |
  |  5 | 2010-01-03 00:00:00 |        1 |        80 |     219 |
  |  6 | 2010-01-03 00:00:00 |        2 |        99 |     220 |
  +----+---------------------+----------+-----------+---------+
  </code></pre>
  
  <p>Currently I am getting the latest data for each device using a query on the <strong>DataLog</strong> table with:</p>
  
  <pre><code> Where DeviceId = 1 ORDER BY timestamp DESC LIMIT 1
  </code></pre>
  
  <p>What I would like is one query to return a list of all devices, with the columns joined with the latest data for each device like this:</p>
  
  <pre><code>+----------+------------+----------------------------+---------------------+-----------+---------+
  | deviceId | deviceName | deviceDescription          | time_stamp          |FuelLevel  | Voltage |
  +----------+------------+----------------------------+---------------------+-----------+---------+
  |        1 | System 1   | Main System in Server Room | 2010-01-03 00:00:00 |        80 |     219 |
  |        2 | System 2   | Outdoor System             | 2010-01-03 00:00:00 |       99  |     220 |
  +----------+------------+----------------------------+---------------------+-----------+---------+
  </code></pre>