繁体   English   中英

Mysql比较行之间的嵌套数据

[英]Mysql Comparing Nested Data Between Rows

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

样本数据

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM