我需要從表中列印出每一代至少有一個學生的每個城市。表很簡單,但我不知道如何從該表中提取每年,因為它的字串。
指數 | 城市
1/2018 | London ;
2/2018 | Paris ;
3/2018 | null;
4/2019 | London ;
4/2020 | London;
在這種情況下,SQL 查詢需要列印 London,因為表中的每一年都有一個學生(2018、2019 和 2020)。我什至不知道如何開始查詢:}
uj5u.com熱心網友回復:
我會嘗試這樣的事情:
select city from
(
select
city, count(distinct cast(SUBSTRING(indeks, LOCATE('/', indeks) 1) as unsigned)) as dy
from cities
group by city
) as t
where
dy = (
select
max(cast(SUBSTRING(indeks, LOCATE('/', indeks) 1) as unsigned))-min(cast(SUBSTRING(indeks, LOCATE('/', indeks) 1) as unsigned)) 1
from cities
)
所以在內部查詢中,它將計算每個城市填充了多少不同的年份
外部過濾器將檢查哪一個擁有所有這些
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/507372.html
上一篇:按年份范圍SQL查詢分組