有两张桌子。我想以这样的方式加入他们,我可以获得以下结果
There are two tables. i want to join them in such a way that i can get the following result
student_mas:: receipt_mas
name class name class month
john 2nd john 2nd JAN
bunny 3rd john 2nd FEB
sunny 4th bunny 3rd FEB
提交特定月份费用的学生将被插入第二个 table,并在第二个 table 的月份列中提及该月份
我想要 JAN 月份未提交费用的学生名单
请帮我。提前致谢。
您可以使用NOT EXISTS
查询
select * from student_mas t
where not exists (
select * from receipt_mas
where name = t.name
and class = t.class
and [month] = 'JAN'
);
Ullas 的回答会很完美,但您可以像下面的方法一样尝试。
DECLARE @student_mas TABLE (
NAME VARCHAR(50)
,class VARCHAR(10)
);
insert into @student_mas
values
('john', '2nd'),
('bunny', '3rd'),
('sunny', '4th');
DECLARE @receipt_mas TABLE (
NAME VARCHAR(50)
,class VARCHAR(10)
,[month] VARCHAR(3)
);
insert into @receipt_mas
values
('john', '2nd', 'JAN'),
('john', '2nd', 'FEB'),
('bunny', '3rd', 'FEB');
SELECT sm.*
FROM @student_mas sm
LEFT JOIN @receipt_mas rm ON sm.NAME = rm.NAME
AND sm.class = rm.class
AND rm.month = 'JAN'
WHERE RM.class IS NULL
with student_mas as
(
SELECT 'JOHN' NAME,'2ND' CLASS FROM DUAL
union all
SELECT 'BUNNY' NAME,'3RD' CLASS FROM DUAL
union all
SELECT 'SUNNY' NAME,'4TH' CLASS FROM DUAL
)
select * from student_mas A
where not exists
(
with receipt_mas as
(
SELECT 'JOHN' NAME,'2ND' CLASS,'JAN' MONTH FROM DUAL
union all
SELECT 'BUNNY' NAME,'3RD' CLASS,'FEB' MONTH FROM DUAL
union all
SELECT 'SUNNY' NAME,'4TH' CLASS,'FEB' MONTH FROM DUAL
)
select * from receipt_mas B
where A.NAME=B.NAME
and A.CLASS=B.CLASS
and B.MONTH='JAN'
)
/
student_mas:: receipt_mas
name class name class month
john 2nd john 2nd JAN
bunny 3rd john 2nd FEB
sunny 4th bunny 3rd FEB
提交特定月份费用的学生将被插入第二个 table,并在第二个 table 的月份列中提及该月份 我想要 JAN 月份未提交费用的学生名单 请帮我。提前致谢。
您可以使用NOT EXISTS
查询
select * from student_mas t
where not exists (
select * from receipt_mas
where name = t.name
and class = t.class
and [month] = 'JAN'
);
Ullas 的回答会很完美,但您可以像下面的方法一样尝试。
DECLARE @student_mas TABLE (
NAME VARCHAR(50)
,class VARCHAR(10)
);
insert into @student_mas
values
('john', '2nd'),
('bunny', '3rd'),
('sunny', '4th');
DECLARE @receipt_mas TABLE (
NAME VARCHAR(50)
,class VARCHAR(10)
,[month] VARCHAR(3)
);
insert into @receipt_mas
values
('john', '2nd', 'JAN'),
('john', '2nd', 'FEB'),
('bunny', '3rd', 'FEB');
SELECT sm.*
FROM @student_mas sm
LEFT JOIN @receipt_mas rm ON sm.NAME = rm.NAME
AND sm.class = rm.class
AND rm.month = 'JAN'
WHERE RM.class IS NULL
with student_mas as
(
SELECT 'JOHN' NAME,'2ND' CLASS FROM DUAL
union all
SELECT 'BUNNY' NAME,'3RD' CLASS FROM DUAL
union all
SELECT 'SUNNY' NAME,'4TH' CLASS FROM DUAL
)
select * from student_mas A
where not exists
(
with receipt_mas as
(
SELECT 'JOHN' NAME,'2ND' CLASS,'JAN' MONTH FROM DUAL
union all
SELECT 'BUNNY' NAME,'3RD' CLASS,'FEB' MONTH FROM DUAL
union all
SELECT 'SUNNY' NAME,'4TH' CLASS,'FEB' MONTH FROM DUAL
)
select * from receipt_mas B
where A.NAME=B.NAME
and A.CLASS=B.CLASS
and B.MONTH='JAN'
)
/