常用 SQL 语句大全

发表于:2007-10-15 16:40

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:网络转载

---全局变量

  ---必须以@@开头

  --IF ELSE  

declare @x int @y int @z int
  select @x = 1 @y = 2 @z=3
  if @x > @y
  print 'x > y' --打印字符串'x > y'
  else if @y > @z
  print 'y > z'
  else print 'z > y'


  --CASE  

use pangu
  update employee
  set e_wage =
  case
  when job_level = ’1’ then e_wage*1.08
  when job_level = ’2’ then e_wage*1.07
  when job_level = ’3’ then e_wage*1.06
  else e_wage*1.05
  end


  --WHILE CONTINUE BREAK  

declare @x int @y int @c int
  select @x = 1 @y=1
  while @x < 3
  begin
  print @x --打印变量x 的值
  while @y < 3
  begin
  select @c = 100*@x + @y
  print @c --打印变量c 的值
  select @y = @y + 1
  end
  select @x = @x + 1
  select @y = 1
  end

--WAITFOR

  --例 等待1 小时2 分零3 秒后才执行SELECT 语句 

waitfor delay ’01:02:03’
  select * from employee


  --例 等到晚上11 点零8 分后才执行SELECT 语句  

waitfor time ’23:08:00’
  select * from employee


  ***SELECT*** 

select *(列名) from table_name(表名) where column_name operator value
  ex:(宿主)
  select * from stock_information where stockid = str(nid)
  stockname = 'str_name'
  stockname like '% find this %'
  stockname like '[a-zA-Z]%' --------- ([]指定值的范围)
  stockname like '[^F-M]%' --------- (^排除指定范围)
  --------- 只能在使用like关键字的where子句中使用通配符)
  or stockpath = 'stock_path'
  or stocknumber < 1000
  and stockindex = 24
  not stock*** = 'man'
  stocknumber between 20 and 100
  stocknumber in(10,20,30)
  order by stockid desc(asc) --------- 排序,desc-降序,asc-升序
  order by 1,2 --------- by列号
  stockname = (select stockname from stock_information where stockid = 4)


  --------- 子查询

  --------- 除非能确保内层select只返回一个行的值,

  --------- 否则应在外层where子句中用一个in限定符 

select distinct column_name form table_name --------- distinct指定检索独有的列值,不重复
  select stocknumber ,stocknumber + 10 = stocknumber + 10 from table_name
  select stockname , stocknumber = count(*) from table_name group by stockname


  --------- group by 将表按行分组,指定列中有相同的值  

having count(*) = 2 --------- having选定指定的组
  select *
  from table1, table2
  where table1.id *= table2.id -------- 左外部连接,table1中有的而table2中没有得以null表示
  table1.id =* table2.id -------- 右外部连接
  select stockname from table1
  union [all] ----- union合并查询结果集,all-保留重复行
  select stockname from table2

62/6<123456>
《2023软件测试行业现状调查报告》独家发布~

精彩评论

  • xiaoyu2006
    2007-11-08 17:37:26

    不错,有指导作用,多谢

  • qicyt1812
    2007-10-29 17:35:03

    数据库想了解很简单,但是想深入学习就比较难咯,不过如果做测试的话基本了解就可以了,要求不是很高。如果想做DBA那就要付出更多的努力才行。

  • vivuzhan
    2007-10-25 11:49:13

    因為沒有深入學過數據庫,所以想問問數據庫很難嘛?  
    如果學,該如何入手,有比較好的辦法嘛??? 請指點. tks

  • wangkai
    2007-10-24 17:29:16

    很好,非常需要

  • lzc8800
    2007-10-17 10:13:01

    Not bad!
    Thanks

  • 李凡123
    2007-10-17 02:33:20

    很好
       能不能多发点啊

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号