常用SQL語句(增刪查改 合併統計 模糊搜尋)

2021-06-08 12:22:24 字數 3757 閱讀 5198

常用sql語句

首行當然是最基本的增刪查改啦,其中最重要的是查。

還有就是一些要注意的地方,就是sql語句對大小寫不敏感,語句中列名對應的值要用單引號''括起來不是雙引號。

sql 使用單引號來環繞文字值。如果是數值,請不要使用引號。

特別是c/c++程式設計師要注意,通常錯誤都是在用字串進行拼接sql語句時,由於雙引號和單引號混用,特別容易出錯。

一、查:1、select 列名稱 from 表名稱,其中列名可以是多個,中間用豆號分開,如select lastname,firstname from persons;

2、select * from 表名稱,表示查詢表中所有的內容,星號(*)是選取所有列的快捷方式;

3、用where 限定搜尋範圍,select 列名稱 from 表名稱 where 列 運算子 值,其中,運算子包括

操作符 描述

= 等於

<> 不等於

> 大於

< 小於

>= 大於等於

<= 小於等於

between     在某個範圍內

like 搜尋某種模式

注釋:在某些版本的 sql 中,操作符 <> 可以寫為 !=。

如:select * from persons where city='beijing'

4、以下說說上面的乙個運算子like

like 操作符用於在 where 子句中搜尋列中的指定模式。select column_name(s)from table_name where column_name like pattern

例如:select * from persons where city like 'n%'

select * from persons where city like '%g'

select * from persons where city like '%lon%'

在以上的三個例子中,都有乙個符號「%」,"%" 可用於定義萬用字元(模式中缺少的字母)。對於第乙個例子,可以理解為查詢city列中的名字中一定要以n開頭的,後面的字母是什麼,不用管;

同理,第二個例子,是搜尋city列中的名字中一定要以g結尾的城市,第三個例子,則是查詢city列中的名字中

名字存在lon的城市。

簡單點來說,這是一種模糊搜尋,而「%」可以代表任何字元。

5、and,or

and 即條件交集,or 即條件並集;

二、改:update 語句用於修改表中的資料。語法:update 表名稱 set 列名稱 = 新值 where 列名稱 = 某值;

列名可以是多個,多個列名之間用豆號分開;

例如:update person set firstname = 'fred' where lastname = 'wilson'

update person set address = 'zhongshan 23', city = 'nanjing' where lastname = 'wilson'

三、刪:delete 語句用於刪除表中的行。語法:delete from 表名稱 where 列名稱 = 值,如:delete from person where lastname = 'wilson' 

刪除所有行,delete from table_name或delete * from table_name(注意,並不刪除表);

四、增:insert into 語句用於向**中插入新的行。語句:insert into 表名稱 values (值1, 值2,....)

或insert into table_name (列1, 列2,...) values (值1, 值2,....)

例如:insert into persons values ('gates', 'bill', 'xuanwumen 10', 'beijing')或

insert into persons (lastname, address) values ('wilson', 'champs-elysees『)

五、sum()函式;

sum 函式返回數值列的總數(總額)。注意,只能統計數值。語法:select sum(column_name) from table_name

例如:select sum(orderprice) as ordertotal from orders;as表示生成的資料的列名是ordertotal

六、count()函式

1、count(column_name) 函式返回指定列的值的數目(null 不計入),即column_name中有多少個不同的值:

select count(column_name) from table_name

如:select count(customer) as customernilsen from orders where customer='carter'

2、count(*) 函式返回表中的記錄數,即表中有多少條記錄:select count(*) from table_name

如:select count(*) as numberoforders from orders

3、count(distinct column_name) 函式返回指定列的不同值的數目:select count(distinct column_name) from table_name

如:select count(distinct customer) as numberofcustomers from orders

注:count()函式可以統計出乙個列中某一值的出現次數,而不限於列的值的資料型別,而sum()函式限定操作的值的型別一定要是數值型別;

七、group by

1、group by 語句用於結合合計函式,根據乙個或多個列對結果集進行分組。通常與sum()、count()等函式一起用。

2、語法:select column_name0, aggregate_function(column_name1) from table_name where column_name operator value group by column_name2

3、與sum()的結合使用,可以根據column_name2列中的值的不同而對column_name1的值進行分組並合計;

例如:select city, sum(input) from person  group by city ;就能按把收入統計並按城市分組,即表中有多少個不同的city就有多少行資料。

4、與count()結合使用,可以統計出某錶中某列中的某值出現的次數.

select a as xm,count(a) as cs from table1 group by a

如某錶table1,

... a ...

... 0 ...

... 1 ...

... 2 ...

... 1 ...

... 0 ...

其中a列只有0,1,2三種值,如何統計各值出現的次數,最好弄成這樣的

xm cs

0 (0出現的次數)   

1 (1出現的次數)

2 (2出現的次數)

5、我們也可以對乙個以上的列應用 group by 語句,就像這樣:

select customer,orderdate,sum(orderprice) from orders group by customer,orderdate

例如:select column1,column2,column3 from table_name1,talbe_name2 where table_name1.column = table_name2.column

注意:兩個表中就有乙個記錄相同資訊的列 column。

基本SQL 語句運算元據增刪查改

1 建立資料庫 create database 資料庫名 如 create database student 2 連線到乙個已經存在的資料庫 use 資料庫名 如 use student 3 刪除資料庫 drop database 資料庫名 如 drop database student 4 建立表...

sql的增刪查改

摘要 php用的最多的是mysql資料庫,在php工具箱的mysql管理器中有乙個phpmyadmin,還可以從adminer中 登陸之後先建立資料庫,再建立乙個表結構,橫著的為一條記錄,豎著的為字段。資料庫可以從編輯等地方改資料,一般是通過sql命令語句中通過常用的語句增刪查改。常用的增刪查改語句...

SQL常用增刪改查語句

1.1插入單行 insert into 表名 列名 values 列值 例 insert into person id,name,age,phone,address values 1 yang 22 123232323 中國上海 例如我只想插入一條資料,而我只知道這個人的名字,於是我也可以插入一條記...