2013-09-30 17 views
6

Bir MYSQL sorgusu kullanarak iki tablodan tarihi almaya çalışıyorum. Onlara katılmak istiyorum categories.cat_id=topics.topic_cat idi. Birden çok giriş aynı topic_cat olabilir, bu yüzden en son SELECT MAX(topic_date) eşit olan SELECT istiyorum.MYSQL JOIN iki tablo tarihine göre ikinci tablodan sonuçları sınırla

Aşağıdaki sorguda, topic_cat başına yalnızca bir sonuç bulunan ve en son tarihe sahip olan sonuçlardan doğru bilgiler gösterilmektedir.

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

Çoklu satırlar topic_cat için aynı değere sahip olabilir, ama sadece almak ve katılmak yalnızca en son MAX (topic_date) ve daha sonra kategoriler tablosundan aşağıdaki bilgileri gösterir bir sorguya katılmak istiyorum.

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; 

Bu sorgu, bir şey dışında doğru bilgileri görüntüler. Topic_cat'i en eski girişle veya MIN(topic_date) ile gösterir. Topic_cat'i en yeni giriş veya MAX(topic_date) ile elde etmek için ama denemeden aşağıdakileri denedim.

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 

Herhangi bir yardım veya öneri çok takdir edilecektir.

Tamam, işte örnek veriler ve ilişkili istenen sonuç.

Tablo 1 = kategoriler

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

Tablo 2 = konular

__________________________________________________ 
| 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  | 
--------------------------------------------------------- 
Yukarıdaki tablolarda göre, aşağıdaki verileri vermek sorgu istiyorum

:

_________________________________________________________________________________________________ 
| 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 umarım şeyleri açıklığa kavuşturur.

+1

örnek verileri ve ilgili istenen sonucu verin. –

+0

'LIMIT' deyimini kullanmaya çalışın. –

+0

En son tarihe kadar sınırlayabilir miyim? – Jimbob

cevap

4

Bu deneyin:

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

için

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

değiştirmeyi deneyin:

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 
İlgili konular