SQL JOIN - 为每个表A返回一行,无论表B是否具有值

时间:2016-04-15 23:17:23

标签: sql sql-server sql-server-2008

下面的查询应该为每个Reading_Type返回一行,加上该Reading_Type和date的已保存读取值,如果没有读取已保存,则返回0。

SELECT
      t.*
    , ISNULL(r.Reading, 0) AS Reading
FROM
    Reading_Type t
LEFT JOIN
    Reading r ON t.Reading_Type_ID = r.Reading_Type_ID
WHERE
    r.Reading_Date = @date
    OR r.Reading_Date IS NULL
  • 如果没有为任何日期保存读数,
  • 如果保存的唯一读数是针对所选日期的话, 可以正常工作。
  • 如果Reading_Type有一个已保存的日期X读数,没有保存的日期为Y的读数,搜索的日期为Y,则工作。

Reading_Type表:

Reading_Type_ID  Reading_Type
-----------------------------
1                Red
2                Blue
3                Green

Reading表(表格为空):

Reading_ID  Reading_Type_ID  Reading  Reading_Date
-----------------------------------------------------

@date = April 15, 2016的查询返回:

Reading_Type_ID  Reading_Type  Reading
----------------------------------------
1                Red           0
2                Blue          0
3                Green         0

Reading表(表格包含4月15日的数据):

Reading_ID  Reading_Type_ID  Reading  Reading_Date
-----------------------------------------------------
1           1                5        April 15, 2016
2           3                8        April 15, 2016

@date = April 15, 2016的查询返回:

Reading_Type_ID  Reading_Type  Reading
----------------------------------------
1                Red           5
2                Blue          0
3                Green         8

@date = April 7, 2016的查询返回:

Reading_Type_ID  Reading_Type  Reading
----------------------------------------
1                Red           0
3                Green         0

第三个查询仍应为Reading_Type = Blue返回一行,其中读取为0。如何修复查询?

3 个答案:

答案 0 :(得分:4)

您的WHERE标准导致了您的过滤器问题(我自己只做了一百万次左右)。试试这个:

SELECT
      t.*
    , ISNULL(r.Reading, 0) AS Reading
FROM
    Reading_Type t
LEFT JOIN
    Reading r ON t.Reading_Type_ID = r.Reading_Type_ID
AND r.Reading_Date = @date

在此实例中省略WHERE子句(除非您想进一步过滤数据)。

以下是一些有助于详细说明此SQL功能的信息:Specifying Joins in FROM or WHERE clauses

答案 1 :(得分:1)

如果r.Reading_Date可以为null,并且您想要包含那些

SELECT t.*, ISNULL(r.Reading, 0) AS Reading
FROM Reading_Type t
LEFT JOIN Reading r 
       ON r.Reading_Type_ID = t.Reading_Type_ID
      AND isnull(r.Reading_Date, @date) = @date

答案 2 :(得分:-1)

查询正在执行您要求的操作。它正在进行左连接(将返回r.Reading_Date中的所有记录以及来自NULL的记录(如果/当存在时)...然后它将应用您的where条件。如果@date既不是SELECT t.*, ISNULL(r.Reading, 0) AS Reading FROM Reading_Type t LEFT JOIN ( SELECT Reading_Type_ID, Reading FROM Reading WHERE Reading_Date = @date OR Reading_Date IS NULL ) r ON t.Reading_Type_ID = r.Reading_Type_ID 也不是<div id = "myCarousel" class = "carousel slide" data-interval="false"> <!-- Carousel indicators --> <ol class = "carousel-indicators"> <li data-target = "#myCarousel" data-slide-to = "0" class = "active"></li> <li data-target = "#myCarousel" data-slide-to = "1"></li> <li data-target = "#myCarousel" data-slide-to = "2"></li> <li data-target = "#myCarousel" data-slide-to = "3"></li> <li data-target = "#myCarousel" data-slide-to = "4"></li> <li data-target = "#myCarousel" data-slide-to = "5"></li> <li data-target = "#myCarousel" data-slide-to = "6"></li> <li data-target = "#myCarousel" data-slide-to = "7"></li> <li data-target = "#myCarousel" data-slide-to = "8"></li> <li data-target = "#myCarousel" data-slide-to = "9"></li> <li data-target = "#myCarousel" data-slide-to = "10"></li> <li data-target = "#myCarousel" data-slide-to = "11"></li> <li data-target = "#myCarousel" data-slide-to = "12"></li> <li data-target = "#myCarousel" data-slide-to = "13"></li> <li data-target = "#myCarousel" data-slide-to = "14"></li> <li data-target = "#myCarousel" data-slide-to = "15"></li> <li data-target = "#myCarousel" data-slide-to = "16"></li> <li data-target = "#myCarousel" data-slide-to = "17"></li> <li data-target = "#myCarousel" data-slide-to = "18"></li> <li data-target = "#myCarousel" data-slide-to = "19"></li> <li data-target = "#myCarousel" data-slide-to = "20"></li> </ol> <!-- Carousel items --> <div class = "carousel-inner"> <div class = "item active"> <img src = "includes/modules/sliders/Business_planning_for_success/Slide1.JPG" alt = "slide"> </div> <div class = "item"> <img src = "includes/modules/sliders/Business_planning_for_success/Slide2.JPG" alt = "slide"> </div> <div class = "item"> <img src = "includes/modules/sliders/Business_planning_for_success/Slide3.JPG" alt = "slide"> </div> <div class = "item"> <img src = "includes/modules/sliders/Business_planning_for_success/Slide4.JPG" alt = "slide"> </div> <div class = "item"> <img src = "includes/modules/sliders/Business_planning_for_success/Slide5.JPG" alt = "slide"> </div> <div class = "item"> <img src = "includes/modules/sliders/Business_planning_for_success/Slide6.JPG" alt = "slide"> </div> <div class = "item"> <img src = "includes/modules/sliders/Business_planning_for_success/Slide7.JPG" alt = "slide"> </div> <div class = "item"> <img src = "includes/modules/sliders/Business_planning_for_success/Slide8.JPG" alt = "slide"> </div> <div class = "item"> <img src = "includes/modules/sliders/Business_planning_for_success/Slide9.JPG" alt = "slide"> </div> <div class = "item"> <img src = "includes/modules/sliders/Business_planning_for_success/Slide10.JPG" alt = "slide"> </div> <div class = "item"> <img src = "includes/modules/sliders/Business_planning_for_success/Slide11.JPG" alt = "slide"> </div> <div class = "item"> <img src = "includes/modules/sliders/Business_planning_for_success/Slide12.JPG" alt = "slide"> </div> <div class = "item"> <img src = "includes/modules/sliders/Business_planning_for_success/Slide13.JPG" alt = "slide"> </div> <div class = "item"> <img src = "includes/modules/sliders/Business_planning_for_success/Slide14.JPG" alt = "slide"> </div> <div class = "item"> <img src = "includes/modules/sliders/Business_planning_for_success/Slide15.JPG" alt = "slide"> </div> <div class = "item"> <img src = "includes/modules/sliders/Business_planning_for_success/Slide16.JPG" alt = "slide"> </div> <div class = "item"> <img src = "includes/modules/sliders/Business_planning_for_success/Slide17.JPG" alt = "slide"> </div> <!-- <div class = "item"> <img src = "includes/modules/sliders/Business_planning_for_success/Slide18.JPG" alt = "slide"> </div> --> <!-- <div class = "item"> <img src = "includes/modules/sliders/Business_planning_for_success/Slide20.JPG" alt = "slide"> </div> --> the "questions" slider starts here <div class = "item"> <img src="../../images/top.jpg"> <div class="col-sm-4" style="color:black;"> <div class=""> <div><img src="../../images/Slide18.png" width="300" height="150"></div> </div> <strong>Scenario</strong> <div style="color: #990000;"> <div><p>Siphelele's Sweets produces and sells fudge bars in Alexandra, Johannesburg.</p> <ul> <li>Each fudge bar is sold at R15</li> <li>Her variable costs per fudge bar amount to R5</li> <li>Siphelele's Sweets expects fixed costs of R30 000 for the year ending 31 Dec, 2016</li> </ul> </div> </div> </div> <div class="col-sm-4"> <table width="734" style="margin-left:10px color:black;"> <form name="test1" action="" > <tr><td height="30" width="578"><label>Questions</label></td></tr> <div> <tr><td height="30" width="578"><label>a.How many bars does Siphelele have to sell to break even? </label> <ul class="nostyle"> <li><input name="q2" type="radio" value="2000" /> <label>2000</label></li> <li><input name="q2" type="radio" value="3000" /> <label>3000</label></li> <li><input name="q2" type="radio" value="1500" /> <label>1500</label></li> <li><input name="q2" type="radio" value="250" /> <label>250</label></li> </ul> </td></tr> </div> <div> <tr><td height="30" width="578"><label>b.What is the break even point in sales given your first answer? </label> <ul class="nostyle"> <li><input name="q3" type="radio" value="30000" /> <label>30000</label></li> <li><input name="q3" type="radio" value="3750" /> <label>3750</label></li> <li><input name="q3" type="radio" value="45000" /> <label>45000</label></li> <li><input name="q3" type="radio" value="22500" /> <label>22500</label></li> </ul> </td></tr> </div> <a class="btn" href="javascript:submitForm();">Check</a> </form> </table> </div> <img src="../../images/down.jpg"> </div> ,那么该查询中的记录将从结果集中排除。

我认为你想要的,如果我已经理解正确的话,就是在子选择中的左联接......所以更像是这样:

<script language="JavaScript">
  function submitForm()
  { 
       if ((document.test1.q2[0].checked == false)&&(document.test1.q2.value == "")){
       alert ("Please answer question a");
       return;
       }

     if (( document.test1.q2.value != "3000")){
       alert ("Oops incorrect answer , please try question a again");
       return;
       }

     if ((document.test1.q3[0].checked == false)&&(document.test1.q3.value == "")){
       alert ("Please answer question b");
       return;
       }

     if ((document.test1.q3.value != "45000")){
       alert ("Oops incorrect answer , please try question b again");
       return;
       }

       else{    

             $("#myCarousel").carousel('next');


     }

  }
 </script>



   </div>

   <!-- Carousel nav -->
   <a class = "carousel-control left" href = "#myCarousel" data-slide = "prev">&lsaquo;</a>
   <a class = "carousel-control right" href = "#myCarousel" data-slide = "next">&rsaquo;</a>

   <!-- Controls buttons -->
   <div style = "text-align:center;">
      <input type = "button" class = "btn-sm prev-slide" onclick="slide()" value = "Previous Slide">
      <input type = "button" class = "btn-sm next-slide" onclick="slide()" value = "Next Slide">

      <script type="text/javascript">

      function correct_answer() {
            // Cycles to the next item
          $(".next-slide").click(function() {
             $("#myCarousel").carousel('next');
          });
      }

      function slide() {

      // Cycles to the previous item
      $(".prev-slide").click(function() {
         $("#myCarousel").carousel('prev');
      });

      // Cycles to the next item
      $(".next-slide").click(function() {
         $("#myCarousel").carousel('next');
      });

   };
</script>