|
今天看到項(xiàng)目代碼里有這條語句,不懂select 1 from XXXXXXX里的1是何意,查了一番才知道: 1、select 1 from mytable;與select anycol(目的表集合中的任意一行) from mytable;與select * from mytable 作用上來說是沒有差別的,都是查看是否有記錄,一般是作條件用的。select 1 from 中的1是一常量,查到的所有行的值都是它,但從效率上來說,1>anycol>*,因?yàn)椴挥貌樽值浔怼?/P> 2、查看記錄條數(shù)可以用select sum(1) from mytable;等價(jià)于select sum(*) from mytable; 3、實(shí)際項(xiàng)目SQL: select c_insrnc_cde, c_nme_cn 二、http://blog.csdn.net/wangyihust/archive/2009/02/05/3863758.aspx 測試場景:(轉(zhuǎn)自網(wǎng)絡(luò)文獻(xiàn)) 1:select 1 from table 增加臨時(shí)列,每行的列值是寫在select后的數(shù),這條sql語句中是1 2:select count(1) from table 不管count(a)的a值如何變化,得出的值總是table表的行數(shù) 3:select sum(1) from table 計(jì)算臨時(shí)列的和 在SQL SERVER中用 1 測試了一下,發(fā)現(xiàn)結(jié)果如下: 1:測試結(jié)果,得出一個(gè)行數(shù)和table表行數(shù)一樣的臨時(shí)列(暫且這么叫,我也不知道該叫什么),每行的列值是1; 2:得出一個(gè)數(shù),該數(shù)是table表的行數(shù); 3:得出一個(gè)數(shù),該數(shù)是table表的行數(shù); 然后我又用“2”測試,結(jié)果如下: 1:得出一個(gè)行數(shù)和table表行數(shù)一樣的臨時(shí)列,每行的列值是2; 2:得出一個(gè)數(shù),該數(shù)是table表的行數(shù); 3:得出一個(gè)數(shù),該數(shù)是table表的行數(shù)×2的數(shù) 然后我又用更大的數(shù)測試: 1:得出一個(gè)行數(shù)和table表行數(shù)一樣的臨時(shí)列,每行的列值是我寫在select后的數(shù); 2:還是得出一個(gè)數(shù),該數(shù)是table表的行數(shù); 3:得出一個(gè)數(shù),該數(shù)是table表的行數(shù)×寫在select后的數(shù) 綜上所述:第一種的寫法是增加臨時(shí)列,每行的列值是寫在select后的數(shù);第二種是不管count(a)的a值如何變化,得出的值總是table表的行數(shù);第三種是計(jì)算臨時(shí)列的和。
語句if not exists(select 1 from deleted d join inserted i on d.ID=i.ID if not exists 如果不存在 |
|
|