[英]mysql-query other table condition PHP
I have this table in mysql called safespot 我在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
+---------+--------+
And this is table users
: 这是表users
:
+----+-------------+-------------+
| id | userid | cash |
+----+-------------+-------------+
| 1 | 1 | 100000 |
| 2 | 2 | 100000 |
| 3 | 3 | 100000 |
+----+-------------+-------------+
how can i do something like 我该怎么做
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)
So basically do a query that also would return it if userid dont exist in safespot table, or if it does, that timestamp is higher than safe_value. 因此,基本上可以执行一个查询,如果用户ID不存在于safespot表中,或者如果确实存在,则该时间戳大于safe_value,该查询也将返回该查询。
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()
)
)
Use WHERE NOT EXISTS
like 使用WHERE NOT EXISTS
像
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())
This returns users where 这将使用户返回
safespot
for the given userid, or 给定的用户ID在safespot
没有条目,或者 safespot
with the value of safe
greater than the current timestamp. 有一个条目safespot
与价值safe
比当前时间戳。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.