Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL JOIN two tables limit results from second table by date

I am trying to retrieve date from two tables using a MYSQL query. I want to join them together were categories.cat_id=topics.topic_cat. Multiple entries may have the same topic_cat, so I only want to SELECT the most recent, which is equal to MAX(topic_date).

The following query shows the correct information from topics, with only one result per topic_cat and that result having the most recent date.

SELECT topic_subject, topic_cat, topic_date
FROM topics
GROUP BY topic_cat DESC

Multiple rows may have the same value for topic_cat, but I only want to retrieve and join only the most recent, MAX(topic_date) and then join to a query which shows the following information from the categories table.

SELECT categories.cat_id, categories.cat_name, categories.cat_description, topics.topic_subject, topics.topic_cat, topics.topic_date, topics.topic_by
FROM categories
LEFT JOIN topics
ON categories.cat_id=topics.topic_cat
GROUP BY cat_id;

This query displays the correct information, except one thing. It shows the topic_cat with the oldest entry, or MIN(topic_date). I have tried the following to get the topic_cat by newest entry or MAX(topic_date), but without success.

SELECT categories.cat_id, categories.cat_name, categories.cat_description
FROM categories
LEFT JOIN (SELECT topic_subject, topic_cat, topic_date, topic_by
FROM topics
GROUP BY topic_cat DESC) AS topics
ON categories.cat_id=topics.topic_cat

Any help or suggestions would be greatly appreciated.

Ok, so here is the sample data and associated desired result.

Table 1 = categories

_______________________________________________________
| cat_id | cat_name      | cat_description             |
-------------------------------------------------------
| 1      |  james        | Some information about james|
-------------------------------------------------------
| 2      |  myo          | Some information about myo  |
-------------------------------------------------------
| 3      |  brandon      | Some information about brandon  |
-------------------------------------------------------

Table 2 = topics

__________________________________________________
| topic_subject |  topic_cat  |  topic_date  |   topic_by  |
----------------------------------------------------------
| marcos        |  2          | 2013-9-28  |  User 1       |
---------------------------------------------------------
| ferdinand     |  2          | 2013-9-29  |  User 2       |
---------------------------------------------------------
|  maria luisa  |  2          | 2013-9-30  |  User 1       |
---------------------------------------------------------
|  Isabella     |  1          | 2013-8-24   |  User 3      |
--------------------------------------------------------
| Carlos        |  3          |  2012-6-21   |  User 2     |
--------------------------------------------------------
|  Enrique      |  3          |  2011-4-2   |  User 3      |
---------------------------------------------------------

I would like the query to return the following data based on the above tables:

_________________________________________________________________________________________________
| cat_id | cat_name      | cat_description                |  topic_subject |  topic_cat  |  topic_date  |   topic_by        |
----------------------------------------------------------------------------------------------------------------
|  1     |  james        | Some information about james   |   Isabella     |  1          | 2013-8-24    |  User 3          |
----------------------------------------------------------------------------------------------------------------
|  2     |  myo          | Some information about myo     |  maria luisa   |  2          | 2013-9-30    |  User 1           | 
----------------------------------------------------------------------------------------------------------------
|  3     |  brandon      | Some information about brandon | Carlos         |  3          |  2012-6-21   |  User 2         |
----------------------------------------------------------------------------------------------------------------

I hope that clarifies things.

like image 649
Jimmy Long Avatar asked Sep 30 '13 00:09

Jimmy Long


People also ask

What is the most efficient way of joining 2 table in same database?

Relational algebra is the most common way of writing a query and also the most natural way to do so. The code is clean, easy to troubleshoot, and unsurprisingly, it is also the most efficient way to join two tables.

Can we apply Group by clause while joining two tables?

SQL Inner Join permits us to use Group by clause along with aggregate functions to group the result set by one or more columns. Group by works conventionally with Inner Join on the final result returned after joining two or more tables.

Can we join more than 2 tables using join?

Join is a binary operation. More than two tables can be combined using multiple join operations. Understanding the join function is fundamental to understanding relational databases, which are made up of many tables.

Can we apply join on 2 tables without any relation?

The answer to this question is yes, you can join two unrelated tables in SQL, and in fact, there are multiple ways to do this, particularly in the Microsoft SQL Server database. The most common way to join two unrelated tables is by using CROSS join, which produces a cartesian product of two tables.


2 Answers

Try This:

###
SELECT * FROM categories c
LEFT JOIN topics t ON c.cat_id = t.topic_cat
WHERE c.cat_id IN (SELECT t1.cat_id FROM (
    SELECT c.cat_id, c.cat_name, MAX(t.topic_date) AS maxdate FROM categories c
    LEFT JOIN topics t ON c.cat_id = t.topic_cat
    GROUP BY c.cat_name
) as t1 WHERE t1.maxdate = t.topic_date OR t.topic_date IS NULL );

### without nulls
SELECT * FROM categories c
    LEFT JOIN topics t ON c.cat_id = t.topic_cat
    WHERE c.cat_id IN (SELECT t1.cat_id FROM (
    SELECT c.cat_id, c.cat_name, MAX(t.topic_date) AS maxdate FROM categories c
    LEFT JOIN topics t ON c.cat_id = t.topic_cat
    GROUP BY c.cat_name
) as t1 WHERE t1.maxdate = t.topic_date);
like image 178
Jason Avatar answered Oct 25 '22 13:10

Jason


Try changing

LEFT JOIN (SELECT topic_subject, topic_cat, topic_date, topic_by
FROM topics
GROUP BY topic_cat DESC) AS topics

to:

LEFT JOIN (SELECT topic_subject, topic_cat, topic_date, topic_by
FROM topics
GROUP BY topic_cat
ORDER BY topic_date DESC
LIMIT 0,1) AS topics
like image 27
Thomas Wood Avatar answered Oct 25 '22 15:10

Thomas Wood