每个modx_site_content记录可能在modx_site_tmplvar_contentvalues中有多个记录。
我需要检索modx_site_tmplvar_contentvalues.value,其中tvv.tmplvarid=3和tvv.tmplvarid=1。如果tvv.tmplvarid是将来的日期,我需要返回tvv.tmplvarid 3的tvv.value,它是用逗号分隔的标记列表。
这个查询不返回我需要的值&我不确定如何得到我想要的值。

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

最后,我只需要返回标签列表(tvv.tmplvarid=3),其中其他相关记录(tvv.tmplvarid=1)是未来的日期。
有什么想法,可以用分组来代替吗?我实际上不需要任何来自modx_site_content表的东西。

最佳答案

所以您需要返回modx_site_tmplvar_contentvalues表中tmplvarid的两行标记,其中modx_site_content的1和3都与同一tmplvarid相关,但只有当3行将来有一个datetime字段时才需要返回?
我将对modx_site_tmplvar_contentvalues选项卡执行两个单独的连接:

SELECT tOne.value, tThree.value
FROM modx_site_tmplvar_contentvalues tOne
INNER JOIN modx_site_content c ON tOne.contentid = c.id
INNER JOIN modx_site_tmplvar_contentvalues tThree ON tThree.contentid = c.id
WHERE c.published = 1
AND tOne.tmplvarid = 1
AND tThree.tmplvarid = 3 AND tThree.date > NOW()

SQL小提琴:http://sqlfiddle.com/#!2/a4031/2

09-26 03:47