从多个表中获取记录的问题

时间:2018-07-20 11:34:45

标签: sql sql-server

我有下面的表格,我试图获得输出,如异常结果所示

+----+------+--------------+
| id | name | bedfrid      |
+----+------+--------------+
| 1  | bed1 | 111111111111 |
+----+------+--------------+
| 2  | bed2 | 222222222222 |
+----+------+--------------+
| 3  | bed9 | 777777777777 |
+----+------+--------------+

DepartmentBeds

+----+------+-------+
| deptid | bedfrid  |
+----+------+-------+
| 1A  | 111111111111|
+----+------+-------+
| 1A  | 222222222222|
+----+------+-------+
| 5B  | 333333333333|
+----+------+-------+

位置

+----+------------+----------------------------+
| deptid | start time |  end time |    bedfrid |
+----+------------+----------------------------+
| 1A  | 0950       |   NULL    | 222222222222  |
+----+------------+----------------------------+
| 5B  | NULL       |   NULL    | 333333333333  |
+----+------+----------------------------------+
| 1A  | NULL      |   NULL    | 111111111111   |
+----+------------+----------------------------+

预期结果

+----+------------+----------------------------+
| 1A  | bed2       |  222222222222 |
+----+------------+----------------------------+

查询为:

select bed.name, bed.id 
from BED bed inner join
     DepartmentBeds dptBEDS
     on dptBEDS.bedfrid = bed.id inner join
     Location loc
     ON loc.bedfrid = dptBEDS.bedfrid
where dptBEDS.id = '1A' AND loc.starttime IS NOT NULL AND loc.endtime IS NULL

我是SQL新手。编写了上面的查询,但没有得到预期的结果。我哪里做错了。

3 个答案:

答案 0 :(得分:0)

我希望这会有所帮助:

select loc.deptid, bed.name, loc.bedfrid
from (Location as loc inner join DepartmentBeds as dp
      on loc.bedfrid = dp.bedfrid) inner join bed
      on bed.bedfrid = dp.bedfrid
where loc.deptid = dp.deptid AND loc.starttime IS NOT NULL AND loc.endtime IS NULL

答案 1 :(得分:0)

这是在OP的预期结果为错误的前提下进行的。他们的预期结果的值为[name],其值为'bed1',但是,[bedfrid]的值为222222222222'bed1'的值为111111111111,其值为[bedfrid],所以我不知道这两个值之间的关系。因此,这可能是因为OP的意思是'bed2',而不是'bed1'此问题已得到纠正):

USE Sandbox;
GO

CREATE TABLE dbo.Bed (id int,
                      [name] varchar(4),
                      bedfrid bigint);
CREATE TABLE dbo.DepartmentBed (deptid char(2),
                                bedfrid bigint);
CREATE TABLE dbo.[Location](deptid char(2),
                            starttime char(4),
                            endtime char(4),
                            bedfrid bigint);

INSERT INTO dbo.Bed (id,
                     [name],
                     bedfrid)
VALUES (1,'bed1',111111111111),
       (2,'bed2',222222222222),
       (3,'bed9',777777777777);

INSERT INTO dbo.DepartmentBed (deptid,
                               bedfrid)
VALUES ('1A',111111111111),
       ('1A',222222222222),
       ('5B',333333333333);

INSERT INTO dbo.[Location] (deptid,
                            starttime,
                            endtime,
                            bedfrid)
VALUES ('1A','0950',NULL,222222222222),
       ('5B',NULL,NULL,333333333333),
       ('1A',NULL,NULL,111111111111);
GO
SELECT DB.deptid,
       B.[name],
       B.bedfrid
FROM dbo.Bed B
     JOIN dbo.DepartmentBed DB ON B.bedfrid = DB.bedfrid
     JOIN dbo.[Location] L ON DB.bedfrid = L.bedfrid
                          AND DB.deptid = L.deptid
WHERE DB.deptid = '1A'
  AND L.starttime IS NOT NULL
  AND L.endtime IS NULL;

GO

DROP TABLE dbo.Bed;
DROP TABLE dbo.DepartmentBed;
DROP TABLE dbo.[Location];

答案 2 :(得分:0)

我认为您的加入有误,下面应该是正确的(测试一下):

select bed.name, bed.id 
from BED bed 
    inner join DepartmentBeds dptBEDS on dptBEDS.bedfrid = bed.bedfrid 
    inner join Location loc ON loc.depid = dptBEDS.depid
where dptBEDS.id = '1A' 
    AND loc.starttime IS NOT NULL 
    AND loc.endtime IS NULL