Sort grouped rows in MySQL statement
I have a MySQL database with the following structure:

**Table `customers`:**

 - id (int)
 - name (string)
 - ...

**Table `orders`:**

 - id (int)
 - customerID (int)
 - timestamp_unix (int)
 - title

Now I want to select all customers as well as their latest order. I tried the following statement:

    SELECT a.id, a.name, b.timestamp_unix, b.title FROM customers AS a JOIN orders AS b ON a.id = b.customerID GROUP BY a.id

This works fine except that I don't get the *latest* order (and its title) but the first one that has been inserted to the database as the first one.

So how can I get the latest order (highest `id` and highest `timestamp_unix`)? For the timestamp only, I could just use `MAX(b.timestamp_unix)` but how do I get the matching `b.title`?

Thank you!
Sort grouped rows in MySQL statement
===

I have a MySQL database with the following structure:

**Table `customers`:**

 - id (int)
 - name (string)
 - ...

**Table `orders`:**

 - id (int)
 - customerID (int)
 - timestamp_unix (int)
 - title

Now I want to select all customers as well as their latest order. I tried the following statement:

    SELECT a.id, a.name, b.timestamp_unix, b.title FROM customers AS a JOIN orders AS b ON a.id = b.customerID GROUP BY a.id

This works fine except that I don't get the *latest* order (and its title) but the first one that has been inserted to the database as the first one.

So how can I get the latest order (highest `id` and highest `timestamp_unix`)? For the timestamp only, I could just use `MAX(b.timestamp_unix)` but how do I get the matching `b.title`?

Thank you!