易截截图软件、单文件、免安装、纯绿色、仅160KB

菜鸟学SQL语句

为了大家更容易理解我举出的SQL语句,本文假定已经建立了一个学生成绩管理数据库,全文均以学生成绩的管理为例来描述。
  1.在查询结果中显示列名:
  a.用as关键字:select name as '姓名' from students order by age
  b.直接表示:select name '姓名' from students order by age
  2.精确查找:
  a.用in限定范围:select * from students where native in ('湖南', '四川')
  b.between...and:select * from students where age between 20 and 30
  c.“=”:select * from students where name = '李山'
  d.like:select * from students where name like '李%' (注意查询条件中有“%”,则说明是部分匹配,而且还有先后信息在里面,即查找以“李”开头的匹配项。所以若查询有“李”的所有对象,应该命令:'%李%';若是第二个字为李,则应为'_李%'或'_李'或'_李_'。)
  e.[]匹配检查符:select * from courses where cno like '[AC]%' (表示或的关系,与"in(...)"类似,而且"[]"可以表示范围,如:select * from courses where cno like '[A-C]%')
  3.对于时间类型变量的处理
  a.smalldatetime:直接按照字符串处理的方式进行处理,例如:
select * from students where birth > = '1980-1-1' and birth <= '1980-12-31'
  4.集函数
  a.count()求和,如:select count(*) from students (求学生总人数)
  b.avg(列)求平均,如:select avg(mark) from grades where cno=’B2’
  c.max(列)和min(列),求最大与最小
  5.分组group
  常用于统计时,如分组查总数:
select gender,count(sno)
from students
group by gender
(查看男女学生各有多少)
  注意:从哪种角度分组就从哪列"group by"
  对于多重分组,只需将分组规则罗列。比如查询各届各专业的男女同学人数 ,那么分组规则有:届别(grade)、专业(mno)和性别(gender),所以有"group by grade, mno, gender"
select grade, mno, gender, count(*)
from students
group by grade, mno, gender
  通常group还和having联用,比如查询1门课以上不及格的学生,则按学号(sno)分类有:
select sno,count(*) from grades
where mark<60
group by sno
having count(*)>1
  6.UNION联合
  合并查询结果,如:
SELECT * from students
W


相关文档:

一个多表嵌套查询的sql语句

select batch_no "批次号",get_id "分类" from (
select(
select plan1.batch_no from product_plan plan1 where plan1.item_no=(select head1.product_code from wo_head head1 where head1.order_no =bbb)) batch_no, 
(select decode(get_id,'BUY','外购件','MAK','自制件','MB','未定') from item where item_ ......

如何写sql的递归查询语句?

cpcbid    cpcbbh          type  parentid  parentbh            cbze
70      2009120001      11      NULL        NU ......

SQL Server连接远程数据源的基本方法

SQL Server连接远程数据源的基本方法有下面三种:
OPENDATASOURCE: The OPENDATASOURCE function is used to specify connection information for a remote data source by specifying the OLE DB provider and an initialization string. OPENDATASOURCE can be used directly within a SELECT, INSERT, UPDATE, or DELET ......

定期删除备份的SQL数据库的脚本

  比如某个数据库下对SQL SERVER的数据库进行了每天的备份,现在要保留7天以内的,其他的删除掉,用ASP可以实现了,但要用到filesystemobject,不大爽
    首先写一个js文件clearDatabaseBackup,内容如下
function deleteBackupFile(beforedays) {
    var fso = new ActiveXObject ......

利用typeperf工具收集SQL Server性能数据

一.利用TypePerf.exe命令行工具把Windows操作系统的性能计数器数据记录到数据库中
 
可以在作业中建立以下脚本
1.启用xp_cmdshell
--默认情况下,sql server2005安装完后,xp_cmdshell是禁用的(可能是安全考虑),如果要使用它,可按以下步骤
 
-- 允许配置高级选项
EXEC sp_configure 'show advanced options ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号