SQL基礎5 資料庫的條件語句

2021-09-26 04:45:04 字數 4131 閱讀 7333

第四篇##

資料庫的基礎資料**本方將分為10個短文發布,此為第四篇,講述了sql條件查詢的一些常用**和對應分析,包括結構型別、約束條件等:

-查詢操作符:

1、in:看字段值在不在資料表中和資料集中。

2、any:查詢結果集中的任何乙個值。

3、all:查詢結果集中的每乙個值。

4、exists:測試子查詢結果集中有沒有記錄。

5、[not]between...and...:測試欄位在不在給定的兩個值中間。

6、is [not] null:測試字段是不是空值。

7、[not]like:測試自段值是否與給定的字元模式匹配。

8、and:測試條件1與測試條件2是否都滿足要求

9、or:測試條件1與測試條件2其中乙個是否滿足要求。

10、not:測試條件是否不滿足要求。

-distinct:用來操作重覆記錄

select distinct s_id from fruits;

-where總的子查詢:

子查詢:寫在()中把內層查詢結果當做外層查詢引數的資料表來用

例1:用in操作符與子查詢語句來查詢所有f_id 對應的f_price在10到20之間的水果記錄。

select * from fruits where f_id in

(select f_id from fruits where f_price between 10 and 20);

例2:用any操作符和子查詢語句來查詢所有f_id對應的f_price在10到20之間的水果記錄。

select * from fruits where f_id = any

(select f_id from fruits where f_price between 10 and 20);

例3:用all操作符與子查詢語句來查詢所有**大於20的記錄。

select * from fruits where f_id > all

(select f_id from fruits where f_price <20);

例4:用exists操作符與子查詢語句來查詢是否存在f_price大於30元的水果記錄

select * from fruits where f_id exists

(select f_id from fruits where f_price > 30);

-as:可以將表重新命名,只在查詢中有效。

select f.* from fruits as f;

-limit:查詢後只顯示limit指定數字的行數結果。

select * from fruits

order by f_price desc

limit 3;

-常用的數學函式:(主要用於處理數字值)

abs(x):返回x的絕對值。

bin(x):返回x的二進位制(oct返回x的8進製,hex返回x的16進製制)。

exp(x):返回值e的(自然對數的底)的x次方。

greatest(x1,x2,....,xn):返回集合中最大的值。

least(x1,x2,....,xn):返回集合中最小的值。

ln(x):返回x的自然對數。

log(x,y):返回x的以y為底的對數。

mod(x,y):返回x/y的餘數。

pi():返回圓周率值。

rand():返回0到1內的隨機值,可以通過提供乙個引數(種子)是rand()隨機數生成器生成乙個指定的值。

floor(x):返回小於x的最大整數值,去掉小數取整。

geiling(x):返回大於x的最小整數值,進一取整。

round(x,y):返回引數x的四捨五入的有y位小數的值,(四捨五入)。

truncate(x,y):返回數字x截短為y位小數的結果。

sign(x):返回數字x的符號的值(正數返回1,負數返回-1,0返回0)。

sqrt:返回乙個數的平方根。

-字串函式:主要用於處理字串值。

ascll(char):返回字元的ascll碼值。

bit_length(str):返回字串的位元長度。

concat(s1,s2,...,sn):將s1,s2,...,sn連線成字串。

concat_ws(sep,s1,s2,...,sn):將s1,s2,...sn連線成字串,並用sep字元分隔。

insert(str,x,y,instr):將字串從第x個位置開始,y個字元長的子串替換為字串instr,返回結果。

find_in_set(str,list):分析逗號間隔的list列表,如果發現str,返回str在list中的位置。

lcase(str)或lower(str):返回字串str中所有字元改變為小寫後的結果。

ucase(str)或upper(str):返回將字串str中所有字元改變為大寫後的結果。

left(str,x):返回字串str中最左邊的x個字元。

right(str,x):返回字串str中最右邊的x個字元。

length(str):返回字串str的字元數。

position(substr,str):返回字串substr在字串str中第一次出現的位置。

quote(str):用反斜槓轉義str中的單引號。

repeat(str,srchstr,rplctr):返回字串str重複x次的結果。

reverse(str)返回顛倒字串str的結果。

ltrim(str):去掉字串str開頭的空格。

rtrim(str):去掉字串str結尾的空格。

trim(str):去除字串首部和尾部的所有空格。

-日期函式:

date_format(date,fmt):依照指定的fmt格式格式化日期值。

from_unixtime(ts,fmt):依照指定的fmt格式,格式化unix時間戳ts。

monthname(date):返回date的月份名(英語月份:october)。

dayname(date):返回date的星期名(英語星期幾:saturday)。

now():返回當前日期和時間。

curdate()和current_date():返回當前日期。

curtime()或current_time():返回當前的時間。

quarter(date):返回date一年中的季度。

week(date):返回一週年中的第幾周(0~53周)。

dayofyear(date):返回date是一年中的第幾天(1~366)。

dayofmonth(date):返回date是乙個月的第幾天(1~31)。

dayofweek(date):返回的date是一周中的第幾天(1~7)。

year(date):返回日期date的年份。

month(date):返回日期date的月份。

day(date):返回日期date的天數部分。

hour(time):返回的time的小時值。

minute(time):返回的time的分鐘值。

second(time):返回的time的秒數值。

date(datetime):返回的datetime的日期值。

time(datetime):返回的datetime的時間值。

-其他函式:

group_concat:返回由屬於一組的列值連線組合而成的結果。常與關鍵字group_by一起使用,能夠將分組後指定的字段值都顯示出來。

例:使用group_concat函式查詢不同s_id下對應的所有f_name資訊

select s_id, group_concat(f_name) from fruits

group by s_id;

cast:將乙個值轉換成指定的資料型別。

-為字段賦值update...set和刪除記錄delete:

例:使用concat函式在f_name欄位值前新增『fruits_』資訊

update fruits set f_name = concat('fruit_',f_name);

delete:刪除資料表中的資料,語法為delete from 表名 [where clause],如果省略where的話則刪除表中所有資料記錄

例:刪除f_id為'b5'的資料記錄

delete from fruits where f_id='b5';

資料庫條件語句

update machinepath select machinepath pcpath,filename from table1 where node2 aaa update table1 set machinepath case when filename like 語文 then 語文 nn學...

Sql 資料庫的基礎語句(一)

對資料的增刪查改串講 在表中新增資料格式 insert into 表名稱 values 值1,值2,在表中刪除資料格式 delete from 表名稱 where 列名稱 值 在表中查詢資料格式 select 列名稱 from 表名稱 可以新增where限制語句 在表中修改資料格式 update 表...

Oracle資料庫Sql語句詳解 條件查詢

第二章 條件查詢 本章目標 where條件查詢 在查詢中使用表示式 運算子 使用like between in進行模糊查詢 where條件查詢 1 請查詢出s emp表中last name為smith的員工的資訊 select from s emp where last name smith 2 請...