mysql sql 子查詢語句 SQL子查詢

2021-10-20 22:39:18 字數 3609 閱讀 1471

子查詢或內部查詢或巢狀查詢在另乙個sql查詢的查詢和嵌入式where子句中。

子查詢用於返回將被用於在主查詢作為條件的資料,以進一步限制要檢索的資料。

子查詢可以在select,insert,update使用,而且隨著運算子如delete語句 =, , >=, <=, in, between 等.

這裡有一些規則,子查詢必須遵循:

子查詢必須被圓括號括起來。

子查詢只能在有一列的select子句中,除非多個列中的子查詢,以比較其選定列主查詢。

order by不能在子查詢中使用,主查詢可以使用order by。group by可以用來在子查詢中如order by執行相同的功能。

返回多於乙個行子查詢只能用於具有多個值運算子,如in操作。

select列表中不能包含到值計算到任何引用 blob, array, clob, 或nclob.

子查詢不能立即封閉在一組函式。

between 操作符不能與子查詢使用;然而,操作符between可以在子查詢中使用。

子查詢select語句:

子查詢最頻繁使用的select語句。其基本語法如下:

select column_name[,column_name]from table1[,table2]where column_name operator(select column_name[,column_name]from table1[,table2][where])

例子:考慮customers表具有以下記錄:

+----+----------+-----+-----------+----------+|id|name|age|address|salary|+----+----------+-----+-----------+----------+|1|ramesh|35|ahmedabad|2000.00||2|khilan|25|delhi|1500.00||3|kaushik|23|kota|2000.00||4|chaitali|25|mumbai|6500.00||5|hardik|27|bhopal|8500.00||6|komal|22|mp|4500.00||7|muffy|24|indore|10000.00|+----+----------+-----+-----------+----------+

現在,讓我們檢查下使用子查詢select語句:

sql>select*from customers

where id in(select id

from customers

where salary>4500);

這將產生以下結果:

+----+----------+-----+---------+----------+|id|name|age|address|salary|+----+----------+-----+---------+----------+|4|chaitali|25|mumbai|6500.00||5|hardik|27|bhopal|8500.00||7|muffy|24|indore|10000.00|+----+----------+-----+---------+----------+

子查詢的insert語句:

子查詢,也可以使用insert語句。insert語句使用從子查詢返回的資料插入到另乙個表中。在子查詢中的所選擇的資料可以與任何型別的字元,日期或數字函式進行修改。

其基本語法如下:

insert into table_name[(column1[,column2])]select[*|column1[,column2]from table1[,table2][where value operator]

示例:考慮customers_bkp表的結構相似於customers表。考慮customers_bkp表的結構相似於customers表。

sql>insert into customers_bkp

select*from customers

where id in(select id

from customers);

子查詢的update語句:

子查詢可以與update語句一起使用。單個或多個表中的列可以使用帶有update語句子查詢時被更新。

其基本語法如下:

update table

set column_name=new_value[where operator[value](select column_name

from table_name)[where)]

示例:假設,我們的customers_bkp表是customers表的備份。

下面的例子是在customers表更新0.25倍薪水,其年齡大於或等於27所有的客戶:

sql>update customers

set salary=salary*0.25where age in(select age from customers_bkp

where age>=27);

這將影響到兩行,最後customers表將有以下記錄:

+----+----------+-----+-----------+----------+|id|name|age|address|salary|+----+----------+-----+-----------+----------+|1|ramesh|35|ahmedabad|125.00||2|khilan|25|delhi|1500.00||3|kaushik|23|kota|2000.00||4|chaitali|25|mumbai|6500.00||5|hardik|27|bhopal|2125.00||6|komal|22|mp|4500.00||7|muffy|24|indore|10000.00|+----+----------+-----+-----------+----------+

子查詢的delete語句:

子查詢可配合使用像上述的任何其他語句delete語句。

其基本語法如下:

delete from table_name[where operator[value](select column_name

from table_name)[where)]

示例:假設,我們的customers_bkp表是customers表的備份。

下面的示例將刪除customers表記錄其年齡大於或等於27所有的客戶:

sql>delete from customers

where age in(select age from customers_bkp

where age>27);

這將影響到兩行,最後customers表將有以下記錄:

+----+----------+-----+---------+----------+|id|name|age|address|salary|+----+----------+-----+---------+----------+|2|khilan|25|delhi|1500.00||3|kaushik|23|kota|2000.00||4|chaitali|25|mumbai|6500.00||6|komal|22|mp|4500.00||7|muffy|24|indore|10000.00|+----+----------+-----+---------+----------+

¥ 我要打賞

糾錯/補充

收藏加qq群啦,易百教程官方技術學習群

注意:建議每個人選自己的技術方向**,同乙個qq最多限加 3 個群。

mysql,SQL語句對於時間的查詢

今天 select from 表名 where to days 時間欄位名 to days now 昨天select from 表名 where to days now to days 時間欄位名 1 7天select from 表名 where date sub curdate interval ...

MySql Sql語句總結

建表語句 create table class id int primary key,class char 255 name varchar 4000 hobby text int 和 integer 是一樣的,只是為了簡寫罷了,主鍵宣告直接跟在定義後面,char和varchar char是固定長度...

MySQL SQL語句優化

檢視表定義 show create table users 檢視表的索引 show index from users 你要獲取第乙個表的所有資訊,你說全表掃瞄快呢還是索引掃瞄快呢?所以當你查詢庫 包括left join中的臨時庫 的所有資訊時,資料庫會選擇最優方法 全表掃瞄!s表dept id na...