我有一个包含多个数据的表。 基本上有很多记录,每条记录都包含一个 uniqueid、postid、posttype 和排名:
现在我对 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/