0

我已经成功地执行了一些表连接并使用数据透视表生成结果(在 Ollie Jones 的帮助下)。sql语句和结果如下。

我希望返回的结果只包含为今天或将来设置的枢轴列“日期”值的行(我认为这是术语!?)。从我所看到WHERE date >= CURDATE()的应该做这项工作,但是因为“日期”在技术上不存在,当我在语句末尾添加到 WHERE 子句时收到执行错误。我不知道如何将它集成到我的第二个子查询中,任何帮助将不胜感激:)

提前致谢

结果集

SELECT 
    content.id as id, content.alias as alias,

    ( 
        SELECT modx_site_tmplvar_contentvalues.value FROM modx_site_tmplvar_contentvalues 
        WHERE modx_site_tmplvar_contentvalues.tmplvarid = 324 
        AND modx_site_tmplvar_contentvalues.contentid = content.id 
     ) AS featured, 

    ( 
        SELECT modx_site_tmplvar_contentvalues.value FROM modx_site_tmplvar_contentvalues 
        WHERE modx_site_tmplvar_contentvalues.tmplvarid = 289 
        AND modx_site_tmplvar_contentvalues.contentid = content.id

     ) AS date

    FROM modx_site_content AS content 

    LEFT JOIN 
        modx_site_tmplvar_contentvalues AS tv_values
        ON tv_values.contentid = content.id 

    WHERE content.parent = 1842 
     AND content.published = 1 

    GROUP BY tv_values.contentid 
    ORDER BY featured DESC, date ASC
4

1 回答 1

1

一个选项是更改“日期”部分并使用“左连接”进行操作:

SELECT 
    content.id as id, content.alias as alias,

    ( 
        SELECT modx_site_tmplvar_contentvalues.value FROM modx_site_tmplvar_contentvalues 
        WHERE modx_site_tmplvar_contentvalues.tmplvarid = 324 
        AND modx_site_tmplvar_contentvalues.contentid = content.id 
     ) AS featured, 

    date_values.value as date

    FROM modx_site_content AS content 

    LEFT JOIN 
        modx_site_tmplvar_contentvalues AS tv_values
        ON tv_values.contentid = content.id

    LEFT JOIN
        modx_site_tmplvar_contentvalues AS date_values
        ON date_values.contentid = content.id

WHERE content.parent = 1842 
    AND content.published = 1 
    AND date_values.tmplvarid = 289
    AND date_values.value >= CURDATE() 

GROUP BY tv_values.contentid 
ORDER BY featured DESC, date ASC
于 2013-06-28T04:23:28.837 回答