mysql查询其他表条件PHP

mysql查询其他表条件PHP,php,mysql,database,Php,Mysql,Database,我在mysql中有一个名为safespot的表 +---------+---------------+ | term_id | userid | safe | +---------+--------|------+ | 1 | 1 | large number, unix timestamp here | 1 | 2 | large number, unix timestamp here | 1 | 3 | large numb

我在mysql中有一个名为safespot的表

+---------+---------------+
| term_id | userid | safe |
+---------+--------|------+
|       1 | 1      |  large number, unix timestamp here
|       1 | 2      | large number, unix timestamp here
|       1 | 3      | large number, unix timestamp here
|       1 | 4      | large number, unix timestamp here
+---------+--------+
这是表用户:

我怎么能做这样的事情

SELECT * FROM `users` where `userid`=1 and `cash`>= 1000 and " userid do not exist in table safespot" or "if the user exists in the safestop table, check if the current timestamp is higher than the safe colum)
所以基本上做一个查询,如果用户ID在safespot表中不存在,或者如果它存在,时间戳高于安全值,也会返回它。

在不存在的地方使用,比如

SELECT * FROM `users` WHERE `userid`=1 AND `cash`>= 1000 AND (userid NOT IN (
        SELECT DISTINCT userid FROM safespot
    ) OR (userid IN (
        SELECT DISTINCT userid FROM safespot WHERE safe < UNIX_TIMESTAMP()
    )
)
在不存在的地方使用,如

这将返回用户所在的位置

safespot中没有给定用户ID的条目,或者 safespot中有一个条目的safe值大于当前时间戳。 这将返回用户所在的位置

safespot中没有给定用户ID的条目,或者 safespot中有一个条目的safe值大于当前时间戳。
但是,它也必须有条件,它存在于另一个表中,它必须检查安全值,使其大于当前时间戳?但是,它也必须有条件,它存在于另一个表中,它必须检查安全值,使其大于当前时间戳?但是,它也必须有条件,如果它存在于另一个表中,则必须检查安全值,使其大于当前时间戳?@maria,如果有帮助,请参阅回答中的编辑。您只需要添加另一个条件。但是,它还必须具有条件,即它存在于另一个表中。它必须检查安全值,使其大于当前时间戳?@maria,如果有帮助,请参阅回答中的编辑。您只需要添加另一个条件。
SELECT * FROM `users` WHERE `userid`=1 AND `cash`>= 1000 AND (userid NOT IN (
        SELECT DISTINCT userid FROM safespot
    ) OR (userid IN (
        SELECT DISTINCT userid FROM safespot WHERE safe < UNIX_TIMESTAMP()
    )
)
SELECT u.* FROM `users` u
where u.`userid`=1 
and u.`cash` >= 1000 
and ( NOT EXISTS ( select 1 from safespot where userid <> u.userid)
or EXISTS (select 1 from safestop where userid = u.userid and safe < CURRENT_TIMESTAMP));
SELECT * FROM users u
LEFT JOIN safespot s ON s.userid = u.userid 
WHERE
    u.userid = 1
    AND u.cash = 1000
    AND (s.userid IS NULL OR s.safe > UNIX_TIMESTAMP())