0

我得到了players表和host字段,我还得到了第二个表bans,其中包含以下字段:host, second_host, ip。我需要检查是否playershost内容字段等于bans表中这三个字段中的任何一个,有一个条件:WHERE expiry >= UNIX_TIMESTAMP()

我已经建立了这个,但是正如你所看到的,bans 表中有三个条件,现在我不知道该怎么办。

SELECT * FROM players
WHERE host NOT IN (
      SELECT host 
      FROM bans 
      WHERE expires >= UNIX_TIMESTAMP()
)

现在我必须对其他三个字段(second_hostip)做同样的事情。这可以在一个查询中完成吗?如果是这样,请发布一个示例。

4

3 回答 3

1

在连接条件下最容易做到这一点:

SELECT players.*
FROM   players
  LEFT JOIN bans 
    ON players.host IN (bans.host, bans.second_host, bans.ip)
   AND (bans.expires >= UNIX_TIMESTAMP() OR bans.expires = -1)
WHERE  bans.PK IS NULL;  -- use primary key from bans table in place of PK

了解SQL 连接

于 2012-05-17T12:23:15.100 回答
1
SELECT * 
FROM   players 
WHERE  NOT EXISTS (SELECT 1 
                   FROM   bans 
                   WHERE  (players.host = bans.host 
                             OR players.host = bans.second_host 
                             OR players.host = bans.ip ) 
                          AND bans.expires >= Unix_timestamp()); 
于 2012-05-17T12:29:47.420 回答
0
SELECT * FROM players
WHERE host NOT IN (
  SELECT host FROM bans WHERE expires >= UNIX_TIMESTAMP()
  UNION
  SELECT second_host from bans WHERE expires >= UNIX_TIMESTAMP()
  UNION
  SELECT ip from bans WHERE expires >= UNIX_TIMESTAMP()
)

或者

SELECT * from players
WHERE host not in (SELECT host.....)
AND host not in (SELECT second_host....)
AND host not in (SELECT ip....)
于 2012-05-17T12:23:34.100 回答