简体   繁体   中英

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:

样本数据

now i am interested in a postid who has posttype=1 and rank bigger than the same postid with posttype=2 basically:

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 ).

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 |
+----------+--------+----------+------+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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