0

这是我的 SQL 查询

select uid,starttime,endtime
from table1
where endtime >= UNIX_TIMESTAMP()
Group by uid
having min(starttime)

当我使用 phpmyadmin 执行查询时,它返回 2 行。但是当我使用 Typo3 执行时,查询返回 27000 行。

$select = "uid,starttime,endtime";
$from ="table1";
$where = "endtime >= UNIX_TIMESTAMP()";
$groupby = "uid having min(starttime)";

$res = $GLOBALS["TYPO3_DB"] -> exec_SELECTquery($select,$from,$where,$groupby);

$sql = $GLOBALS["TYPO3_DB"] -> SELECTquery($select,$from,$where,$groupby);

echo $sql;
echo mysql_num_rows($res);

如果我删除了typo3中的having子句。查询返回 2 行,但不是好的。

如何使用 having 子句执行此查询?

感谢您的帮助

4

3 回答 3

1

您可以使用$GLOBALS['TYPO3_DB'] -> sql_query()这是一个 API 函数。您应该避免使用 nativ mysql-functions。如果你使用 nativ mysql-functions,当你更新到 TYPO3 6.2 时,你会在没有警告的情况下遇到麻烦。如果你使用 API 函数,如果它被弃用,你会得到一个信息。

于 2013-10-16T07:14:02.300 回答
1

看起来exec_SELECTquery()不支持HAVING子句,所以你需要用完整的写出查询。

如果您尝试获取每个用户的最短开始时间的行,则正确的查询是:

SELECT uid,starttime,endtime
from table1 t1
JOIN (SELECT uid, min(starttime) minstart
      FROM table1
      where endtime >= UNIX_TIMESTAMP()
      GROUP BY uid) t2
ON t1.uid = t2.uid AND t1.starttime = t2.starttime
于 2013-10-15T19:47:59.160 回答
-1

正如文档所说, HAVING 关键字后跟 where 条件。( http://dev.mysql.com/doc/refman/5.0/en/select.html ) 这意味着 MySQL 将您的 "min(starttime)" 表达式解释为 where 条件,并将其评估为 1 (true)对于 min(starttime) 的所有非零正值。

例如,“HAVING count(*) > 10”或“HAVING min(starttime) < now()”或其他更有趣的条件。

所以,把你想要的条件放在 HAVING 之后,你就会得到你想要的任何东西。

为了清楚起见,对于任何感到困惑的人,这里有一个完整的例子:

select uid,starttime,endtime
from table1
where endtime >= UNIX_TIMESTAMP()
Group by uid
having min(starttime) > datediff(now(), interval 1 week);

在此示例中,“min(starttime) > datediff(now(), interval 1 week)”是 HAVING 关键字之后的 where 条件。

于 2013-10-15T19:37:53.813 回答