![](/img/trans.png)
[英]SQL query to only return the rows that have the highest value in a certain column
[英]SQL Query to only return certain rows depending on value of column
我有一個類似於以下表格:
cycle | id | name
------|-------|------
0 | 0012 | bob
s | 0012 | bob
1 | 0012 | bob
0 | 6208 | sally
1 | 6208 | sally
0 | 3276 | jane
s | 3276 | jane
1 | 3276 | jane
0 | 8736 | harry
s | 8736 | harry
我需要一個查詢,將導致:
cycle | id | name
------|-------|------
s | 0012 | bob
0 | 6208 | sally
s | 3276 | jane
s | 8736 | harry
因此,僅返回“循環”值為“ s”的行,除非特定“ id”沒有“ s”的值,在這種情況下,它應返回“循環”值為“ 0”的行。
SELCET cycle, id, name from tbl1 as t1 where cycle='s' or
(cycle=0 and (select count(cycle) from tbl1 as t2 where t2.id = t1.id and t2.cycle='s') <= 0)
group by t1.cycle,t1.id;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.