码迷,mamicode.com
首页 > 其他好文 > 详细

链接查询 变量

时间:2016-09-04 12:51:55      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:

引用上一篇随笔的例子

一,链接查询

--查询人员表中的所有数据,并把bc换成文本,加上bceo;
select code,name,sex,age,(select  bname from bumen where bumen.bcode=renyuan.bc) as 部门,(select bceo from bumen where bumen.bcode=renyuan.bc) from renyuan
--可以用链接查询(如下)
select renyuan.code,name,sex,age,bumen.bname,bceo from renyuan,bumen where renyuan.bc=bumen.bcode
--也可以用join on
--格式:select........from table1 join table2 on table1.?=table2.?(外键关系)
select renyuan.code,name,sex,age,bumen.bname,bceo from renyuan join bumen on renyuan.bc=bumen.bcode
--注意,因为有外键关系这个条件,查询结果只出现满足外键关系的学生信息,如果有个学生的信息,不完整,有null值,那么查询结果就不显示。如果要求显示出来,必须在join前面加上full。

left join...on.(左边的表的内容全部显示,右边表没有匹配的就不显示)

;right jion...on..(跟left join 相反)

 --union,只有在列的数据类型一致的情况下才能连接起来
--查找年龄大于40的员工和年龄小于30的的员工的姓名
select name from yuangong where age>40 union select name from yuangong where age<30

二,变量

--设置变量declare @变量名  数据类型     set @变量名=‘赋值‘
declare @a int
set @a=4
print @a
--输出结果为:4
--if表达式
--正规方法
if 条件表达式
   begin
   sql语句
  end
else
  begin
  sq语句
  end
--如果sql语句只有一条,可以省略begin end
--while语句
while 条件表达式
begin
执行操作
end
--case when 示例
CASE sex
WHEN 1 THEN 
WHEN 2 THEN 
ELSE 其他 
END

完!!

 

链接查询 变量

标签:

原文地址:http://www.cnblogs.com/wwz-wwz/p/5838953.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!