SQL查詢之基礎 語法和概念 完整版

2021-04-07 07:35:05 字數 3806 閱讀 5683

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

檔案內容:

--顯示所有列

--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 子句,它返回被連線的兩個表所有資料行的笛卡爾積,

返回到結果集合中的資料行數等於第乙個表中符合查詢條件的資料行數乘以第二個表中符合查詢條件的資料行數*/

/*自連線 

連線自身 */

/*

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

sql基礎語法 聯接查詢

交叉聯接 1.不帶where條件的,將返回兩個表的 行乘積 select c.e.from sales.customers c cross join hr.employees e 2.帶where 條件的,交叉聯接不能使用on select e1.empid,e1.firstname,e1.last...

sql連線查詢基礎概念

交叉連線即笛卡爾積,是指兩個關係中所有元祖的任意組合。一般情況下,交叉查詢是沒有實際意義的。select from student cross join course1 等值連線 列出被連線表中的所有列,包括其中的重複列。連線條件中的運算子為 這種查詢方式形成的中間表為兩個表的笛卡爾積,有inner...

SQl語法之select查詢

語法1 基礎查詢 語法 select 要查詢的東西 from 表名 特點 通過select查詢完的結果 是乙個虛擬的 不是真實存在 要查詢的東西 可以是常量值 可以是表示式 可以是字段 可以是函式 語法2 條件查詢 條件查詢 根據條件過濾原始表的資料,查詢到想要的資料 語法 select 要查詢的字...