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 列存在,它就是成立的,所以改为下面的。

  • 相关阅读:
    iOS很重要的 block回调
    怎样写具体设计文档
    ORM框架
    RapidXml用法
    【Android Training
    ORACLE触发器具体解释
    LeetCode 131 Palindrome Partitioning
    Git管理工具对照(GitBash、EGit、SourceTree)
    Android下将图片载入到内存中
    怎样破解邮箱password
  • 原文地址:https://www.cnblogs.com/ZQiuMei/p/3375958.html
Copyright © 2011-2022 走看看