How to select different categories in categories table

Hello, i want to select diffent items from cars table.

So what i want to do is, to select from each item only 1 latest added cars in table cars > row name.
Like here below.

Databse: row name:

Bmw 318 > last added
Bmw 320
Bmw 330
Mercedes Sl > last added
Mercedes Cls
Mercedes AMG
Audi 3 > last added
Audi 4
Audi 5
Skoda 1 > last added
Skoda 2
Skoda 3

Echo: Bmw 318 - Mercedes Sl - Audi 3 - Skoda 1

I hope i explained well enough.
Thanks in Advance

Sorry to say that you haven’t… First, are we really talking PHP or SQL (querying the database)?

Second, is “last added” a text in a field? Or the value of the field would be, as an example “BMW318 > last added”?

Also, the structure of the table(s) involved would be handy

Sorry for the bad explanation, i mean instead of doing this here below something better and faster.

$bmw= mysqli_query($con,“SELECT * FROM cars WHERE name=‘bwm’ ORDER BY date DESC LIMIT 1”);
$mercedes= mysqli_query($con,“SELECT * FROM cars WHERE name=‘mercedes’ ORDER BY date DESC LIMIT 1”);
$audi= mysqli_query($con,“SELECT * FROM cars WHERE name=‘audi’ ORDER BY date DESC LIMIT 1”);
$skoda= mysqli_query($con,“SELECT * FROM cars WHERE name=‘skoda’ ORDER BY date DESC LIMIT 1”);

And echo it out like here below, like that the page load is very slow.

while($row = mysqli_fetch_array($bwm)) {
echo $row[‘name’] . " " . $row[‘date’];


while($row = mysqli_fetch_array($mercedes)) {
echo $row[‘name’] . " " . $row[‘date’];


while($row = mysqli_fetch_array($audi)) {
echo $row[‘name’] . " " . $row[‘date’];


Is there a other way to do this and faster ?


Hmm, this seems to be SQL, and an interesting problem. Looking around, it seems you need to use a subquery to make sure you’re going to get the most recent record when you group records by name. I have a similar database of cars, and got decent results with this query:

from (select name,max(date) as latest
        from cars group by name) as y 
inner join cars as x on and = y.latest
order by make

For any kind of explanation of what is happening here, look at the page I got it from: - it seems that you use the subquery to get the distinct list of makes, and the latest timestamp for each make (the max() function gives that), then the main query just finds the record that matches the make and timestamp. Obviously the data in your ‘date’ column will need to be unique for each make.

That would give you a set of results giving a distinct record for each different row in the “name” column, and the remainder of the information from the row that has the highest value of the “date” column. Precisely which fields you return is down to the first line - it seems that you have to specify each field, not use *, though I might be wrong about that.