SQL查詢之基礎(文法和概念)完整版

來源:互聯網
上載者:User

注:下面的內容都是再SQLServer2000的查詢分析器裡面執行的,--是注釋

大家可以把下面的內容儲存為尾碼名為sql的文字檔,然後在SQLServer2000的查詢分析器裡開啟,

因為這篇文章主要說的是SQL的一些查詢的文法和概念,所以有些查詢可能一點現實意義都沒有,只是為了說明查詢的文法。至於有關一些數學基礎可以看前兩天的《資料庫之關係代數(理論)》那篇文章,其它如果還想知道查詢的過程中資料庫到底是怎麼操作的這類問題,大家還是參考參考別的書吧,等我研究下再貼出來,希望大家好好學習,天天向上,我也是,每天好好學習了!!

檔案內容:

--顯示所有列
--select * from Employees;

--按順序顯示某些列
--select EmployeeID,LastName,FirstName,BirthDate from Employees;

--指定列的別名(兩種方式)
-- select EmployeeID as 員工編號,BirthDate as 生日 from Employees;
-- select 員工編號=EmployeeID,生日=BirthDate from Employees;

--限制返回的行數
-- select top 5* from Employees;
-- select top 5 percent* from Employees;
 
--選擇不同的列
 --國家不同
-- select  distinct Country as 國家 from Employees;
 --國家和稱呼至少一個不同
-- select  distinct Country as 國家,TitleOfCourtesy as 稱呼 from Employees;

--對查詢的結果排序
/*
 select EmployeeID as 員工編號, LastName+' '+FirstName as 員工姓名,BirthDate as 出生日期,City as 城市
   from Employees
   where City='London' order by BirthDate ;
 
 --對不同的列採用不同的排序
 select EmployeeID as 員工編號, LastName+' '+FirstName as 員工姓名,BirthDate as 出生日期,City as 城市
   from Employees
   where City='London' order by FirstName, BirthDate desc;
*/
--對查詢的結果進行運算
--
-- use pubs;
-- select title as 書目,price as 單價,ytd_sales as 銷售量,price*ytd_sales as 銷售額 from titles;
--帶有條件的查詢
/*  select EmployeeID as 員工編號, LastName+' '+FirstName as 員工姓名,BirthDate as 出生日期,City as 城市
   from Employees
   where City='London' ;

 select EmployeeID as 員工編號, LastName+' '+FirstName as 員工姓名,BirthDate as 出生日期,City as 城市 ,Country as 國家
   from Employees
 where Country like 'US%'; 
 
 select EmployeeID as 員工編號, LastName+' '+FirstName as 員工姓名,BirthDate as 出生日期,City as 城市 ,Country as 國家
   from Employees
 where Country in ('USA','UK');
 
 --複雜的條件查詢,注意邏輯關係,要加括弧,電腦裡面or的優先順序比 and 高
 select EmployeeID as 員工編號, LastName+' '+FirstName as 員工姓名,PostalCode as 郵編,Country as 國家
   from Employees
 where
 (
 PostalCode='98033'
 or PostalCode='98122'
 or PostalCode='SW1 8JR'
 )
 and Country='USA';
 
*/

--使用內串連查詢
--查詢產品表和供應商表,得到公司和公司相應的產品;
-- use Northwind;
/* select p.SupplierID as 供應商編號,s.CompanyName as 公司名稱,p.ProductName as 產品名稱,p.UnitPrice as 價格
  from Products p, Suppliers s where p.SupplierID=s.SupplierID;
*/
--想要查詢作者和作者寫的書,發現兩張表之間沒有聯絡,但是表titleauthor串連了兩張表,一般當兩者之間關係是多對多時會採用第三張表,
/* use pubs;
 select a.au_lname+' '+a.au_fname as 作者,t.title as 書名 from authors a,titles t,titleauthor ta
 where a.au_id=ta.au_id and t.title_id=ta.title_id;
 
*/ 

--使用外串連
--左外串連,這裡只介紹一下文法,其實下面的查詢和上面的一樣,因為Products表和Suppliers表的SupplierID是一一對應的,
/* use Northwind;
 select p.SupplierID as 供應商編號,s.CompanyName as 公司名稱,p.ProductName as 產品名稱,p.UnitPrice as 價格
 from Products p
 left outer join Suppliers s
 on p.SupplierID=s.SupplierID;
*/
 
--右外串連
/* use Northwind;
 select p.SupplierID as 供應商編號,s.CompanyName as 公司名稱,p.ProductName as 產品名稱,p.UnitPrice as 價格
 from Products p
 right outer join Suppliers s
 on p.SupplierID=s.SupplierID;
*/

--全外串連
/* use Northwind;
 select p.SupplierID as 供應商編號,s.CompanyName as 公司名稱,p.ProductName as 產品名稱,p.UnitPrice as 價格
 from Products p
 full outer join Suppliers s
 on p.SupplierID=s.SupplierID; 
*/ 
 

/*
聯集查詢
  UNION運算子可以將兩個或兩個以上上SELECT語句的查詢結果集合合并成一個結果集合顯示,即執行聯 合查詢。
UNION的文法格式為: select_statement UNION [ALL] selectstatement [UNION [ALL] selectstatement][…n]
其中selectstatement為待聯合的SELECT查詢語句。 ALL選項表示將所有行合并到結果集合中。不指定該項時,
被聯集查詢結果集合中的重複行將只保留一行。
 注:由於沒有合適的例子,所以就不舉例了,下次有了再告訴大夥
*/
 
/*
交叉查詢
交叉串連 交叉串連不帶WHERE 子句,它返回被串連的兩個表所有資料行的笛卡爾積,
返回到結果集合中的資料行數等於第一個表中符合查詢條件的資料行數乘以第二個表中符合查詢條件的資料行數
*/

/*
自串連 
串連自身

*/

/*
子查詢
其實子查詢就是一次查詢的結果作為下一次查詢的輸入
*/

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.