我在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
+---------+--------+


这是表users

+----+-------------+-------------+
| id |    userid   |    cash     |
+----+-------------+-------------+
|  1 |     1       |    100000   |
|  2 |     2       |    100000   |
|  3 |     3       |    100000   |
+----+-------------+-------------+


我该怎么做

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表中不存在,或者如果确实存在,则该时间戳大于safe_value,该查询也将返回该查询。

最佳答案

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())


这将使用户返回


给定用户名的safespot中没有条目,或者
safespot中有一个条目,其值safe大于当前时间戳。

关于php - mysql-query其他表条件PHP,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32441960/

10-16 17:24