Senne Vandenputte Senne Vandenputte - 17 days ago 8
MySQL Question

MYSQL: how to retrieve all table rows + only the last row where column equals

OK, a bit of a complicated query, for me anyway.

I'm trying to build a MYSQL query to retrieve all the rows from a DB, but I only want the last row where the column 'topic' equals 'This Week'.

So, what I mean is this:

I have a DB table with all my blog articles. Every week, I write a summary of the aviation news from the past week. I now want to show all the blog posts on the index page, but I also only want to show the last 'This Week' post.

This is the query I use now to retrieve all the rows:

SELECT * FROM articles WHERE published = '1' ORDER BY date_time DESC LIMIT 10


So I need a query that returns all the rows from my table 'articles' + only the last row where 'topic' = 'This Week'.

Anyone who can help me with this?

Answer

you may use Unions ( something like this

SELECT * FROM articles WHERE published = '1' ORDER BY date_time DESC LIMIT 10
UNION
SELECT * FROM articles WHERE WHERE topic="This Week" order by ID desc LIMIT 1

you may use UNION or UNION ALL whichever suits your need

you may wanna check the actual query and format it as per your needs