[英]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.