第2章 查詢基礎 SQL基礎教程

2021-09-22 18:30:53 字數 4473 閱讀 7597

select 《列名》,……

from 《表名》;

select product_id,product_name,purchase_price

from product;

查詢多列時,需要使用逗號進行分隔。查詢結果中列的順序和select 子句中的順序相同查詢全部的列:

select *

from 《表名》;

select

*from product;

原則上以子句為單位進行換行(子句過長時,為方便起見可以換行)

-- sql 語句可以使用 as 關鍵字為列設定別名(as也可省略)

select product_id as id,product_name as name,purchase_price as price

from product;

orselect product_id id,product_name name,purchase_price price

from product;

-- 別名可以使用中文

select product_id as 商品編號,product_name as 商品名稱,purchase_price as 進貨單價

from product;

-- 在sql語句中使用字串或者日期常數時,必須使用單引號 (')將其括起來

select

'商品'

as string,

38as number,

'2009-02-24'

asdate

,product_id,product_name

from product;

select

distinct product_type

from product;

-- 在使用distinct時,null也被視為一類資料。null存在於多行中時,也會被合併為一條null資料

select

distinct purchase_price

from product;

-- distinct也可以在多列之前使用。此時,會將多個列的資料進行組合,將重複的資料合併為一條

-- distinct關鍵字只能用在第乙個列名之前

select

distinct product_type,regist_date

from product;

select 《列名》, ……

from 《表名》

where 《條件表示式》;

-- where和excel中根據過濾條件對行進行過濾的功能是相同的

select product_name,product_type

from product

where product_type=

'衣服'

;

-- 首先通過where子句查詢出符合指定條件的記錄,然後再選取出select語句指定的列

select product_name

from product

where product_type=

'衣服'

;

sql中子句的書寫順序是固定的,不能隨意更改

where子句必須緊跟在from子句之後,書寫順序發生改變的話會造成執行錯誤

● 1行注釋

書寫在「-- 」之後,只能寫在同一行

● 多行注釋

書寫在「/」和「/」之間,可以跨多行

-- sql 語句中可以使用計算表示式

select product_name,sale_price,sale_price*

2 sale_price_x2

from product;

sql 中也可以像平常的運算表示式那樣使用括號 ( )所有包含 null 的計算,結果肯定是 null

/*

可以通過使用沒有from子句的select語句來實現某種業務

例如,不管內容是什麼,只希望得到一行臨時資料的情況

*/select

(100

+200)*

3as calculation;

-- where子句的條件表示式中也可以使用計算表示式

select product_name,sale_price,purchase_price

from product

where sale_price-purchase_price>=

500;

create

table chars(

chr char(3

)primary

key)

;insert

into chars

values

('1'),

('2'),

('3'),

('10'),

('11'),

('222');

-- 字串型別的資料原則上按照字典順序進行排序,不能與數字的大小順序混淆

select chr

from chars

where chr>

'2';

select product_name,purchase_price

from product

where purchase_price is

null

;select product_name,purchase_price

from product

where purchase_price is

notnull

;

select product_name,product_type,sale_price

from product

where

not sale_price>=

1000

;

-- and 運算子在其兩側的查詢條件都成立時整個查詢條件才成立

select product_name,purchase_price

from product

where product_type=

'廚房用具'

and sale_price>=

3000

;

-- or 運算子在其兩側的查詢條件有乙個成立時整個查詢條件都成立

select product_name,purchase_price

from product

where product_type=

'廚房用具'

or sale_price>=

3000

;

-- and運算子的優先順序高於or運算子。想要優先執行or運算子時可以使用括號

select product_name,product_type,regist_date

from product

where product_type=

'辦公用品'

and(regist_date=

'2009-09-11'

or regist_date=

'2009-09-20'

);

not、and 和 or 稱為邏輯運算子邏輯就是對真值進行操作的意思

真值就是值為真(true)或假(false)其中之一的值

商品「叉子」和「原子筆」的進貨單價(purchase_price)為null

這時真值是除真假之外的第三種值——不確定(unknown)

與通常的邏輯運算被稱為二值邏輯相對,只有sql中的邏輯運算被稱為三值邏輯

product表中設定not null約束的原因:

原本只有4行的真值表,如果要考慮null的話就會增加為3×3=9行

看起來也變得更加繁瑣,考慮null時的條件判斷也會變得異常複雜

SQL基礎教程

sql是用於訪問和處理資料庫的標準的計算機語言。什麼是sql?sql指結構化查詢語言 sql是我們有能力訪問資料庫 sql是一種更ansi的標準計算機語言 sql能做什麼?sql面向資料庫執行查詢 sql可從資料庫取回資料 sql可在資料庫中插入新的記錄 sql可以更新資料庫中的資料 sql可以從資...

SQL基礎教程

1.sql 指的是?structured query language 2.哪個 sql 語句用於從資料庫中提取資料?select 3.哪條 sql 語句用於更新資料庫中的資料?update 4.哪條 sql 語句用於刪除資料庫中的資料?delete 5.哪條 sql 語句用於在資料庫中插入新的資料...

第7章練習題 SQL基礎教程

7.1 請說出下述 select 語句的結果 使用本章中的product表 select from product union select from product intersect select from product order by product id 執行如上select語句得到的結...