2012-06-28 44 views
0

有一些麻煩我在這裏這個查詢&想知道如果有人可以幫助:MySQL查詢,如何根據同一表中不同記錄中的條件選擇數據?

我想選擇所有從modx_site_tmplvar_contentvalues的tvv.value [S],其中tvv.tmplvarid = 1和TVV。 tvv.tmplvarid = 3的值是大於或等於今天的日期。

因此,基本上每個已發佈的modx_site_content.id都有一個父節點'24',在modx_site_tmplvar_contentvalues表中將有至少兩個條目,其中一個tmplvarid爲'1',其值將爲逗號分隔列表,另一個爲'3'的tmplvarid將是一個日期戳。

我需要選擇從modx_site_tmplvar_contentvalues表中的所有值,其中所述tmplvarid爲「1」 [逗號分隔的列表],其中,所述第二條目tmplvarid等於「3」是日期大於等於今天和modx_site_content條件更大發布= '1' 和家長= '24'

,這裏是我的查詢[不起作用]

SELECT sc.id, sc.pagetitle, tvv.value, tvv.tmplvarid, tvv.id 
FROM modx_site_content sc 
left join modx_site_tmplvar_contentvalues tvv on tvv.contentid = sc.id 
where published = '1' 
and parent = '24' 
and (tvv.tmplvarid = '3' or tvv.tmplvarid = '1') 
and tvv.value >= curdate() 
group by sc.id 
order by sc.id 

任何幫助嗎?

回答

1

如果我的理解是正確的,你要基於兩個條件得到數據:

- tmplvarid = '1' 
- tmplvarid = '3' and value >= curdate() 

如果是的話,試試這個:

SELECT sc.id, sc.pagetitle, tvv.value, tvv.tmplvarid, tvv.id 
FROM modx_site_content sc 
left join modx_site_tmplvar_contentvalues tvv on tvv.contentid = sc.id 
where published = '1' 
and parent = '24' 
and ((tvv.tmplvarid = '3' and tvv.value >= curdate()) or (tvv.tmplvarid = '1')) 
order by sc.id 

糾正我,如果我誤解你的問題。

+0

接近,我已經很遠試過正是這個,但它返回從modx_site_tmplvar_contentvalues表兩行,我只需要一個包含CSV值[行與 –

+0

你能澄清在幹嘛的「1」 tmplvarid行您希望獲取數據的條件?只有tmpvarid = 1就夠了,否則s.t? – Thinhbk

+0

我會嘗試:modx_site_content表中的每個項目在modx_site_tmplvar_contentvalues表中都會有[至少] 2個條目。 tmplvarid爲3的條目是一個日期,告訴我什麼時候發生了事件。我需要的數據是第二個條目[tmplvarid = 1]的值,其中關聯的tmplvarid = 3將來。 –

相關問題