SELECT blabla
FROM a INNER JOIN b ON a.a = b.b

Is what I have. However I'd like to sort the results DESC by ID. I'd like to select last [variable x 10] of items sorted by ID DESC. Variable provided by PHP (I'll sort things out, I just need help with the query itself).

Recommended Answers

All 3 Replies

SELECT blabla
FROM a INNER JOIN b ON a.a = b.b
ORDER BY a.id DESC LIMIT 10;

If you're creating the SQL statement in code just replace the 10 with whatever the result of (variable x 10) is.

Here's the problem DESC LIMIT (variable x 10) in case of 5 it would be DESC LIMIT 50. It will give last 50 results, I need 10 results after these 50.

Something like DESC LIMIT from (variable x 10) to (variable x 10 + 10)

You need ... LIMIT offset, rowCount -- ex:

SELECT blabla
FROM a INNER JOIN b ON a.a = b.b
ORDER BY a.id DESC LIMIT 50, 10

will give you at most 10 rows, starting with row 50.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.