Answer the question
In order to leave comments, you need to log in
Complicated SQL query to display categories by dates?
Good day! There is a table of products:
Products
-----------------------------------
id | category_id | name | date |
----------------------------------
and category table:
Category
---------- --
id | name |
------------
Need to output:
-------------------
id | name | date |
--------------------
where name, id is taken from the category and date from the product. That is the sequence of actions:
SELECT category_id, name from Products WHERE date in (SELECT dates.date FROM (SELECT date FROM Products GROUP BY date) as dates ORDER BY dates.date DESC LIMIT 3))
Answer the question
In order to leave comments, you need to log in
Something like:
select p.id as id, p.name as P_NAME, p.date as DATE from Products p
LEFT JOIN Category c
ON c.id=p.category_id
order by p.date DESC
LIMIT 3
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question