MYSQL JOIN两个表限制第二个表的日期结果

时间:2013-09-30 00:52:17

标签: mysql sql select join

我正在尝试使用MYSQL查询从两个表中检索日期。我想和他们一起加入categories.cat_id=topics.topic_cat。多个条目可能具有相同的topic_cat,因此我只想选择最新的,等于MAX(topic_date)

以下查询显示了主题中的正确信息,每个topic_cat只有一个结果,并且该结果具有最新日期。

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

对于topic_cat,多行可能具有相同的值,但我只想检索并加入最新的MAX(topic_date),然后加入查询,该查询显示来自类别表的以下信息。

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;

此查询显示正确的信息,除了一件事。它显示带有最旧条目的topic_cat,或MIN(topic_date)。我已经尝试过以下方法来获取最新条目或MAX(topic_date)的topic_cat,但没有成功。

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

非常感谢任何帮助或建议。

好的,这里是样本数据和相关的预期结果。

表1 =类别

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

表2 =主题

__________________________________________________
| 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      |
---------------------------------------------------------

我希望查询根据上表返回以下数据:

_________________________________________________________________________________________________
| 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         |
----------------------------------------------------------------------------------------------------------------

我希望澄清事情。

2 个答案:

答案 0 :(得分:5)

试试这个:

###
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);

答案 1 :(得分:2)

尝试更改

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

为:

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