PDO SQL查询不限制

时间:2013-09-18 03:47:20

标签: php mysql sql pdo limit

我正在尝试从查询中获取结果,但LIMIT无法正常工作。当我放置LIMIT 10时,它不会返回任何结果。我在这里错过了什么吗?

这是我的代码。我想弄清楚这种奇怪行为的原因是什么。

$username = "derek";
$query = $conn->prepare('SELECT * FROM notifications WHERE (needs=:username OR worker=:username1) ORDER BY CASE WHEN needs=:username2 THEN needsread ELSE workerread END, time DESC LIMIT 10');

$query->bindParam(':username', $username);
$query->bindParam(':username1', $username);
$query->bindParam(':username2', $username);
$query->execute();

1 个答案:

答案 0 :(得分:1)

结束了我的if语句。我得到了结果,但在通过我的if语句过滤后,我想要显示的10个结果不应该显示。所以在将我的查询修复到数据库之后就可以了。这是我的疑问:

$workneed = "workneed";
$follow="follow";

$query = $conn->prepare('SELECT * FROM notifications WHERE CASE WHEN needs=:username THEN type=:workneed END OR CASE WHEN worker=:username THEN type=:follow END ORDER BY CASE WHEN needs=:username THEN needsread ELSE workerread END, time DESC LIMIT 10');
$query->bindParam(':username', $username);
$query->bindParam(':workneed', $workneed);
$query->bindParam(':follow', $follow);
$query->execute();