常用SQL語句

2022-07-13 02:09:08 字數 3368 閱讀 8213

title: 常用sql語句

toc: true

date: 2018-09-29 11:55:20

categories:

tags:

select column1, column2, ...

from table_name;

這裡,column1,column2,...是要從中選擇資料的表的欄位名稱。如果要選擇表中可用的所有字段,請使用以下語法:

select * from table_name;
select distinct語法用於僅返回不同的(different)值。

select distinct column1, column2, ...

from table_name;

可以用count獲取不同值的數量:

select count(distinct country) from customers;
where子句用於提取滿足指定標準的記錄,where子句不僅用於select語法,還用於update,delete語法等。

select column1, column2, ...

from table_name

where condition;

例如:

select * from customers

where country='mexico';

where子句中可以使用以下運算子:

運算子描述=等於

<>

不等於。注意:在某些版本的sql中,這個操作符可能寫成!=

>

大於<

小於》=

大於等於

<=

小於等於

between

在某個範圍內

like

搜尋某種模式

in為列指定多個可能的值

where子句可以與and,or和not運算子結合使用。

and和or運算子用於根據多個條件篩選記錄:

如果條件不為真,則利用not運算子顯示記錄。

and語法

select column1, column2, ...

from table_name

where condition1 and condition2 and condition3 ...;

or語法

select column1, column2, ...

from table_name

where condition1 or condition2 or condition3 ...;

not語法

select column1, column2, ...

from table_name

where not condition;

order by 關鍵字用於按公升序或降序對結果集進行排序。

order by 關鍵字預設情況下按公升序排序記錄。

如果需要按降序對記錄進行排序,可以使用desc關鍵字。

select column1, column2, ...

from table_name

order by column1, column2, ... asc|desc;

例如:

select * from customers

order by country desc;

update 語句用於更新表中已存在的記錄。

update table_name

set column1 = value1, column2 = value2, ...

where condition;

where子句指定哪些記錄需要更新。如果省略where子句,所有記錄都將更新!

栗子:

update customers

set contactname='juan'

where country='mexico';

delete 語句用於刪除表中的行。

delete from table_name

where condition;

where子句指定需要刪除哪些記錄。如果省略了where子句,表中所有記錄都將被刪除!

栗子:

delete from customers

where customername='alfreds futterkiste';

insert into 語句用於向表中插入新記錄。

insert into 語句可以用兩種形式編寫。

指定要插入資料的列的名稱,提供要插入的值:

insert into table_name (column1, column2, column3, ...)

values (value1, value2, value3, ...);

如果要為表中的所有列新增值,則不需要在sql查詢中指定列名稱。但是,請確保值的順序與表中的列順序相同。insert into語法如下所示:

insert into table_name

values (value1, value2, value3, ...);

null用於表示缺失的值。

資料表中的 null 值表示該值所處的字段為空。

具有null值的字段是沒有值的字段。

如何測試null值?

使用比較運算子(例如=,《或<>)來測試null值是不可行的。

我們將不得不使用is null和is not null運算子。

is null語法

select column_names

from table_name

where column_name is null;

is not null語法

select column_names

from table_name

where column_name is not null;

sql常用sql語句

1 查詢某個庫中所有的表名字 select name from sysobjects where xtype u and name dtproperties order by name 2 得到資料庫中所有使用者檢視 select name from sysobjects where xtype v...

常用sql語句

t sql語句複製表的方法 我在sql server 2000中有現個資料庫datahr及demo,它們的結構是一樣,其它有乙個表名為 gbitem.現在我想將demo資料庫的表名 gbitem的全部內容複製到datahr資料庫的表名為 gbitem中。請問此t sql語句應該怎麼寫?謝謝高人指點!...

常用SQL語句

查詢 sp who 中的結果值。因為儲存過程不能查詢,先轉為臨時表再查詢。declare tb table spid varchar 100 ecid varchar 100 status varchar 100 loginame varchar 100 hostname varchar 100 b...