SQL條件語句實例
在SQL語句中,SQL條件語句無疑是使用最廣,最常見的SQL語句之一,下面就將為您介紹幾個使用SQL條件語句,供您學習參考。
----查詢StudentMarks 最大的前三條記錄
select top 3 StudentID,StudentName,Mark from StudentMarks order by Mark desc
----查詢StudentMarks 的第30~40條記錄
1、從前30條的后面, 取前10條
select top 10 * from StudentMarks where StudentID not in(select top 30 StudentID from StudentMarks)
2、先正序取40條, 再反序取10條
select top 10* from (select top 40* from StudentMarks order by Mark ) a
order by Mark desc
3、、利用臨時表
select top 40 * into #Stm from StudentMarks order by Mark
select top * from #Stm order by Mark desc
----查詢和London同屬于一個國家的城市的客戶
select distinct * from customers where country in
( select country from customers where city='London')
----查詢和London,Paris同屬于一個國家的城市的客戶
select * from customers where country in
(select country from Customers where City in ('London','Paris'))
----查詢單價大于Dofu的產品名
select ProductName from Products where UnitPrice>
(select UnitPrice from Products where ProductName='Tofu')
【編輯推薦】
SQL整體更新的方法