Home [SQL] Select 1、Select null
Post
Cancel

[SQL] Select 1、Select null

Exists 搭配 Select 1、Select null

select 1select unll一般用來當條件使用,比如:

  • exists(select 1 from table)
  • exists(select null from table)
1
2
3
4
select A.負責人ID, A.負責人姓名
from [負責人清單] A
where exists 
(select 1 from [販賣資料] B where B.負責人ID = A.負責人ID)

exists語句用來判斷()內的表達式是否存在返回值,如果存在返回true,不存在返回false,所以上面語句select 1select null,返回什麼數據不重要。

什麼是 Select 1

如果資料表裡有記錄,select 1語句會返回相應的記錄行數的結果集,裡面的值都是1。
所以select 1這裡的1不是第一個欄位,它可以替換成任意的常數,比如:select 2,返回的結果集裡面的值就都是2。

Select 1 究竟有什麼用?

Select 1 一般用來當條件使用,比如 exists(select 1 from table)
select 1 的效率比 select 欄位名select *快,因為不用查字典表。

Select Null

select nullselect 1類似,只是返回的值變成null

Count(1)

效率: Count(1) > Count(欄位名) > Count(*)

[SQL] 取得件數 Count()
SQL笔记-select 1与select null

This post is licensed under CC BY 4.0 by the author.