zoukankan      html  css  js  c++  java
  • sql语句里的if else

      因为需要一个查询语句,能同时从三张表中查询一个相同字段的值,只查出来一次即可,这三张表除了这个列名称一致以外,其它没有任何关系,因为只查询一个结果,所以用到了if  else,刚开始以为是if exists ,检查这个值是否存在,后来发现exsits是查询这个列是否存在,如下:

    if exists (select studentType from dbo.StudentGrow_HealthDiseasesRecord)
    select studentType from dbo.StudentGrow_HealthDiseasesRecord
    else if exists (select studentType from dbo.StudentGrow_HealthFitnessTest)
    select studentType from dbo.StudentGrow_HealthFitnessTest
    else if exists (select studentType from dbo.StudentGrow_HealthHealthChecklist )
    select studentType from dbo.StudentGrow_HealthHealthChecklist

    即使没有任何数据,它也是成立的,不符合想要的结果,就改成了如下:

    if  (select studentType from dbo.StudentGrow_HealthDiseasesRecord)>0
    select studentType from dbo.StudentGrow_HealthDiseasesRecord
    else if  (select studentType from dbo.StudentGrow_HealthFitnessTest)>0
    select studentType from dbo.StudentGrow_HealthFitnessTest
    else if  (select studentType from dbo.StudentGrow_HealthHealthChecklist )>0
    select studentType from dbo.StudentGrow_HealthHealthChecklist

    因为studentType 刚好类型是int,所以就 这样判断了。

    在用exists的时候,它就会只执行第一个if,因为只要studentType 列存在,它就是成立的,所以改为下面的。

  • 相关阅读:
    Mininet 搭建自定义网络
    struts 2 三目运算
    shell 变量自增(转)
    Java DES 加密和解密源码(转)
    java调用shell脚本
    shell 学习
    debian安装jdk6
    linux(debian) 安装jdk
    利用SecureCRT上传、下载文件(使用sz与rz命令)
    oracle之报错:ORA-00054: 资源正忙,要求指定 NOWAIT
  • 原文地址:https://www.cnblogs.com/ZQiuMei/p/3375958.html
Copyright © 2011-2022 走看看