select在工作中最常用的sql語句


很多小伙伴都知道,作為運維,我們平時使用最多的數據庫語句就是查詢,掌握最常用的select語句可以讓我們在trouble shooting的時候更加快速,高效。

以下就是我歸納出的幾個最常見的select sql語句 : 

1. 查找:select * from table1 where field1 like ’%value1%’ — like的語法很精妙,查資料!
2. 排序:select * from table1 order by field1,field2 [desc] (select * from table order by 3 desc)- 數字3代表第三列
3. 總數:select count as totalcount from table1
4. 求和:select sum(field1) as sumvalue from table1
5. 平均:select avg(field1) as avgvalue from table1
6. 最大:select max(field1) as maxvalue from table1
7. 最小:select min(field1) as minvalue from table1
8. 隨機取出10條數據:
select top 10 * from tablename order by newid()
9. 隨機選擇記錄:
select newid()
10. 列出數據庫里所有的表名:
select name from sysobjects where type=’U’ // U代表用戶
11. 列出表里的所有的列名:
select name from syscolumns where id=object_id(‘TableName’)
 -- MSSQL
     列出表里的所有的列名:desc tablename -- Oracle
12. between的用法 :
between限制查詢數據范圍時包括了邊界值,not between不包括
select * from table1 where time between time1 and time2
select a,b,c, from table1 where a not between 數值1 and 數值2
13. in 的使用方法:
select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM