php - mysql-查询其他表条件PHP

标签 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 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)

所以基本上做一个查询,如果 safespot 表中不存在 userid 也会返回它,或者如果存在,则时间戳高于 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 中没有给定用户 ID 的条目,或者
  • safespot 中有一个条目,其 safe 的值大于当前时间戳。

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

相关文章:

php - 如何计算 mysql 表中的特定值并用 php 回显,varchar 在表中有多少次?

PHP pdo : prepare() vs transactions

php - iOS 推送通知的服务器错误

php - 如何从可调用对象中获取类

mysql - 为辅助表生成的主键在 @OneToMany 关系上为空

Mysql IMAGE表设计

Mysql,无法创建表

mysql - 如何让 Django 使用不受支持的 MySQL 驱动程序,例如 gevent-mysql 或 Concurrence 的 MySQL 驱动程序?

sql - Access 中的组值范围

php - 使用一张表将不同的 MIME 类型上传到数据库