我不知道如何在sql中寫入查詢。
這是我到目前爲止所嘗試過的。我可以在WHERE子句中使用CASE表達式嗎?
where
case
when a.book_id like 'AB%' then a.book_id = b.school_id, --1
when a.book_id like 'CB%' then a.book_id = b.college_id. --2
end
殼體1和2解釋。
1-我相信很好。
2-時a.book
與字母CB開始e.g CBQ123
然後只取Q123
= b.college_id
。
b.college_id在它前面沒有CB。
編輯添加例如
select
a.Name,
a.ID,
a.Due,
b.school_id,
b.college_id
from Student a and FinishedStudent b
where
case
when a.book_id like 'AB%' then a.book_id = b.school_id, --1
when a.book_id like 'CB%' then a.book_id = b.college_id. --2
end
如果a.book = CBQ111然後Q111是FinishedStudent表不CBQ11,所以我只是需要比較最後3個字符。
編輯與案例2例
when a.book_id ='CBQ111' then a.book_id(Q111) = b.college_id. --2
請提供採樣數據和一個例子的結果集像[這個問題](http://stackoverflow.com/questions/12358842/how-to-get-distinct-rows-with-max-value/ )。您可以在標題中指定SQL Server,但在標記中指定MySQL和Oracle。你能澄清你正在使用哪種產品? –
爲什麼不能在查詢的select部分做同樣的事情? –
book_id爲何等於school_id或college_id?我在這裏看到嚴重的數據模型問題。此外,爲什麼它標記了SQL Server和Oracle?這兩個平臺的語法可能會有所不同。 –