选择查询中的日期比较返回错误结果

时间:2016-07-14 07:03:17

标签: php mysql select

我正在使用以下查询来显示记录。

$query = mysqli_query($con, "SELECT * FROM episode where episodeDate < '$loaddate' 
and dramaID != (select dramaID from drama where popularity = '5') 
and dramaID != (select dramaID from drama where popularity = '6') 
order by episodeDate desc");
  //number of rows
 echo $rowCount = mysqli_num_rows($query);

其中loaddate是mysql数据库中的DATE VARIABLE。 episodeDate也是如此。现在,问题是$ rowCount返回&#39; 0&#39; 0如果episodeDate小于变量loaddate,则数据库有超过15条记录。

可能是什么问题?连接正常,查询也没有返回任何错误。

2 个答案:

答案 0 :(得分:0)

这两个子查询很容易被一个连接替换,它们只能在子查询返回一行的地方工作:查询仍将在实例中运行,并返回一个。

确保您的日期格式为&#34; YYYY-MM-DD HH:MM:SS&#34;:

$query = mysqli_query($con, "
SELECT * 
FROM episode
INNER JOIN drama 
ON episode.dramaID = drama.dramaID 
AND drama.popularity NOT IN ('5','6')
WHERE episode.episodeDate < '$loaddate' 
ORDER BY episode.episodeDate DESC");
//number of rows
echo $rowCount = mysqli_num_rows($query);

答案 1 :(得分:0)

我不认为日期比较必须与&#34;没有结果&#34;做一些事情。有趣的是找出有多少记录选择其他两个子查询从戏剧中选择dramaID,其中受欢迎=&#39; 5&#39;并从戏剧中选择dramaID,其中受欢迎程度=&#39; 6&#39;。 如果他们每人带一条记录,那么试试

"SELECT * FROM episode where episodeDate < '$loaddate' order by episodeDate desc"

如果不是,您可能需要将查询更改为

"SELECT * FROM episode where episodeDate < '$loaddate' 
and dramaID not in (select dramaID from drama where popularity = '5') 
and dramaID not in (select dramaID from drama where popularity = '6') 
order by episodeDate desc"