MYSQL學習筆記(十)使用子查詢

2021-08-04 05:59:51 字數 594 閱讀 6397

利用子查詢進行過濾

select ——,——

from ——

where —— in (select ——

from ——

where —— in (select ——

from ——)

where ——=——);

在select語句,子查詢總是從內向外處理

作為子查詢的select語句只能查詢單個列

雖然子查詢一般與in操作符結合使用,但也可用於測試等於、不等於等

作為計算字段使用子查詢

select ——,

——,

(select count(*)

from ——

where ——=——) as ——

from ——

order by ——;

此select語句返回三列,其中第三列為子查詢,對資料行進行計算

完全限定列名的使用

where order.cust_id=customers.cust_id

where cust_id=cust_id

第一條語句相比第二條限定了cust_id的列表**

MySQl學習筆記(子查詢)

修改資料表 新增單列 alter table tbl name add column col name column definition first after col name 省略first after col name將預設你所新增的列位於所有列的最後面 例 alter table user...

MySQL學習筆記 子查詢

子查詢是將乙個查詢語句巢狀在另乙個查詢語句中。內層查詢語句的查詢結果,可以作為外層查詢語句提供條件。insert into tbl name col name,select.create table if notexists tbl name create definition,select sta...

MySQL學習筆記(5)子查詢

測試子查詢 測試由in引發的子查詢 select from emp where depid in select id from dep select from emp where depid not in select id from dep 由exists 引發的子查詢 select from e...