简体   繁体   English

Mysql比较行之间的嵌套数据

[英]Mysql Comparing Nested Data Between Rows

I have a table with multiple data. 我有一个包含多个数据的表。 Basically there are lot of records, each containing an uniqueid, postid, posttype and rank: 基本上有很多记录,每个记录包含一个uniqueid,postid,posttype和rank:

样本数据

now i am interested in a postid who has posttype=1 and rank bigger than the same postid with posttype=2 basically: 现在我对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

Hope im clear any help is appreciated thank you 希望我清除任何帮助,谢谢

You have to make a join on the same table to check your conditions, and select the second ( d2 ). 您必须在同一张表上进行联接以检查您的条件,然后选择第二个( 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;

Outputs: 输出:

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

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

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