0

so I have a table with topics and I am trying to select the latest topic from every category.

category_id is the category and date is the date created.

I cant actually find out what is the command I have to execute in order to get the latest topic of the category and then select its data.

Thanks in advance

Edit: I want to find the proper command so I can select topic with id 2 from category 1 and topic with id 3 from category 2

Hope it helps you understand what am I talking about.

id   category_id   content   date
1    2             bla bla   12-1-2017 22:54 
2    1             bla bla   12-1-2017 22:58 - latest
3    2             bla bla   12-1-2017 22:57 - latest
4    1             bla bla   12-1-2017 22:55

1 Answers1

0

You can perform multiple WHERE conditions in your select.

SELECT * FROM usertable WHERE FirstName = 'John' AND LastName = 'Smith';

Or even conditional based on other factors:

SELECT * FROM Users WHERE (FirstName = 'John' OR FirstName = 'Jennifer') AND (LastName = 'Smith' OR LastName = 'Jones');

Here's an easy to follow guide

Note the AND condition in the SQL command.

SO based on what you want:

$sql = "SELECT * from `table` WHERE id = 2 AND category_id = 3"
scoopzilla
  • 887
  • 5
  • 15
  • The closest approach of what I have on my mind right is something like `SELECT * FROM topics WHERE category_id=2 AND date=MIN(date)` but it doesnt work at all – George Chareas Jan 12 '17 at 20:12
  • what is the order of your columns in the SQL? – scoopzilla Jan 12 '17 at 20:15
  • Ok I misunderstood what you wanted. Actually @PM 77's answer seems correct: `SELECT MIN(``date``) FROM table GROUP BY category_id` – scoopzilla Jan 12 '17 at 20:16
  • Ive actually figured it out by myselft thanks for the answer mate. `SELECT * FROM topics WHERE category_id=3 ORDER BY date DESC LIMIT 1` works too :) – George Chareas Jan 12 '17 at 20:20