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’)

 

posted @ 2018-07-22 09:49  Xbingbing  阅读(2105)  评论(0编辑  收藏  举报