简体   繁体   中英

SQL server query for two tables

I need help to make query :)

I have two tables, TableA and TableB. In TableB have many records related to TableA

TableA:

Id  Label
 1   abc
 2   def
 3   gef

TableB:

Id tableA_id  due_at
 1    1        2016-05-25
 2    1        2015-05-25
 3    1        2014-05-25

I want to make the query where I am able to check every record of TableB. Below is my current query but it checks only last record instead of all

SELECT a.id, b.due_at
FROM tableA a
  LEFT JOIN tableB b ON b.tableA_id = a.id
where (b.due_at > '2016-05-26' OR b.due_at is null)

检查条件b.due_at > '2016-05-26'根据信息,您在**TableB**没有记录,其due_at greater than '2016-05-26'

First, you want to select the column a.id which already exists in the table B, Thus Join is not needed in your case.

Also, the Result of your query should be zero as you don't have data in table b having b.due_at > '2016-05-26', they are all < '2016-05-26'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM