我有以下sql,它不适用于where条件。
SELECT t.PROJID,sum(t.UNITS) AS totalunits, sum(t.COST) AS totalcost, project, max(t.DATE) as lastupdated, PROJCODE
FROM `projectcosts` `t`
left join projects on projects.PROJID = t.PROJID
where lastupdated LIKE '%2014-06-11%'
GROUP BY t.PROJID
答案 0 :(得分:1)
您不应在日期使用like
。正确的解决方案是简单地截断日期并进行比较。但是,您需要使用数据中的字段:
SELECT t.PROJID, sum(t.UNITS) AS totalunits,
sum(t.COST) AS totalcost, project, max(t.DATE) as lastupdated, PROJCODE
FROM `projectcosts` `t` left join
projects on projects.PROJID = t.PROJID
where date(t.date) = date('2014-06-11')
GROUP BY t.PROJID;
或者,如果您在lastupdated
上有索引,则使用范围可以使用索引:
SELECT t.PROJID, sum(t.UNITS) AS totalunits,
sum(t.COST) AS totalcost, project, max(t.DATE) as lastupdated, PROJCODE
FROM `projectcosts` `t` left join
projects on projects.PROJID = t.PROJID
where t.date >= date('2014-06-11') and t.date < date('2014-06-12')
GROUP BY t.PROJID;
您可能真的想要使用having
,这是通过使用列别名而不是基列来建议的。在那种情况下:
SELECT t.PROJID, sum(t.UNITS) AS totalunits,
sum(t.COST) AS totalcost, project, max(t.DATE) as lastupdated, PROJCODE
FROM `projectcosts` `t` left join
projects on projects.PROJID = t.PROJID
GROUP BY t.PROJID
HAVING date(lastupdated) = date('2014-06-11');
答案 1 :(得分:0)
喜欢永远不能和日期字段一起使用。 如果您要将类型更改为vachar,则可能