0

我需要能够检索为 Null 或具有绑定用户名的表行。有人可以解释正确的sql语句吗?

$value = $db_site->prepare('SELECT * FROM tablename WHERE user IS NULL AND user = :user');
$value->bindValue(':user', $user, PDO::PARAM_STR);
$value->execute();

如果重要,则通过 for 循环发送检索到的信息。

4

2 回答 2

3

use OR instead of AND:

SELECT * FROM tablename WHERE user IS NULL OR user = :user

by using AND you are saying that the user column should be null and equal to :user at the same time.

于 2013-01-08T07:59:32.693 回答
1

I need to be able to retrieve the table rows that are Null OR have the bound user name.

'SELECT * FROM tablename WHERE user IS NULL AND user = :user'

Do you notice the difference? ;-)

于 2013-01-08T07:59:19.190 回答