1

我有一条 SQL 语句:

$sql = "SELECT * FROM user_events WHERE userID =" . $uid . " OR groupID = (IN ($gId) WHERE userID IS NULL) ORDER BY timestamp DESC";

打印时看起来像这样:

SELECT * FROM user_events WHERE userID = 34 OR groupID =(IN (44,45) WHERE uID IS NULL) ORDER BY timestamp DESC

我要做的是从数据库中选择userId匹配特定变量或groupID匹配 CSV 变量的任何userIDNULL

谁能解释我在这里做错了什么?我被卡住了!

4

3 回答 3

9

IN是操作员,而不是……那个。你也想要AND,没有WHERE

$sql = "SELECT * FROM user_events
        WHERE userID = $uid
           OR groupID IN ($gId) AND userID IS NULL
        ORDER BY timestamp DESC";
于 2012-08-29T14:43:09.380 回答
1
$sql = "SELECT * FROM user_events 
       WHERE userID =" . $uid . " 
       OR (groupID IN ($gId) AND userID IS NULL) ORDER BY timestamp DESC";
于 2012-08-29T14:44:11.423 回答
0

我认为你想要类似的东西:

SELECT * FROM user_events 
WHERE (userID = 34 OR groupID IN (44,45)) and uID IS NULL
ORDER BY timestamp DESC

在 PHP 中,这可能看起来像:

$sql = "SELECT * FROM user_events WHERE (userID =". $uid . " OR groupID IN ($gId)) AND userID IS NULL ORDER BY timestamp DESC"; 
于 2012-08-29T14:44:59.540 回答