php - Mysql 比较行之间的嵌套数据

标签 php mysql sql

我有一个包含多个数据的表。 基本上有很多记录,每条记录都包含一个 uniqueid、postid、posttype 和排名:

sample data

现在我对 posttype=1 的 postid 感兴趣,并且排名比 posttype=2 的相同 postid 大 基本上:

select * from data where postid=254454 and posttype=1 and rank > same post id but posttype=2 and smaller rank

希望我清楚任何帮助,谢谢

最佳答案

您必须在同一个表上进行联接以检查您的条件,然后选择第二个 (d2)。

select d2.* 
from data d1
inner join data d2 on d2.postid=d1.postid and d2.posttype=2 and d2.rank<d1.rank
where d1.postid=254454 and d1.posttype=1;

输出:

+----------+--------+----------+------+
| uniqueid | postid | posttype | rank |
+----------+--------+----------+------+
|        2 | 254454 |        2 |    2 |
+----------+--------+----------+------+

关于php - Mysql 比较行之间的嵌套数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48694001/

相关文章:

mysql - Web 应用程序中的数据库连接

php - MySQL - LEFT OUTER JOIN 上无限运行的查询

php - PHP 中的 Linux 命令

mysql - 当列名相同时,对连接表的 PDO 查询

sql - 如何找出导致 Oracle 死锁的原因

MySQL BETWEEN 查询 - 哪一部分使用索引?

PHP - 替换多维数组中的数据,特定键

php - 什么是处理文件上传的好 PHP 库?

mysql - UNIX_TIMESTAMP MYSQL,无法获得正确的查询

php - Mysql使用内存限制