MySQL Query,如何根据同一个表中不同记录中的条件选择数据?

时间:2012-06-28 04:25:49

标签: mysql

在查询此问题时遇到一些问题&想知道是否有人可以提供帮助:

我正在尝试从modx_site_tmplvar_contentvalues中选择所有tvv.value [s],其中tvv.tmplvarid = 1且tvv.tmplvarid = 3的tvv.value是大于或等于今天的日期。

所以基本上发布的每个modx_site_content.id的父级都是'24',modx_site_tmplvar_contentvalues表中至少有2个条目,其中tmplvarid为'1',其值为逗号分隔列表,另一个为逗号分隔列表'3'的tmplvarid将是一个日期戳。

我需要从modx_site_tmplvar_contentvalues表中选择tmplvarid为'1'[逗号分隔列表]的所有值,其中第二个条目tmplvarid等于'3'是大于等于今天的日期,modx_site_content条件是published ='1'和parent ='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 个答案:

答案 0 :(得分: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

如果我误解了你的问题,请纠正我。