【问题标题】:There are two tables. i want to join them in such a way that i can get the following result有两张桌子。我想以这样的方式加入他们,我可以获得以下结果
【发布时间】:2016-03-16 05:37:37
【问题描述】:
student_mas::                  receipt_mas
name    class                   name     class     month
john     2nd                   john       2nd       JAN
bunny    3rd                   john       2nd       FEB
sunny    4th                   bunny      3rd       FEB

提交特定月份费用的学生被插入到第二个表中,在第二个表的月份列中提及月份 我想要 1 月份未提交费用的学生名单 请帮我。提前致谢。

【问题讨论】:

    标签: sql sql-server vb.net visual-studio-2010


    【解决方案1】:

    您可以使用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'
    );
    

    SQL Fiddle demo

    【讨论】:

    • 非常感谢亲爱的。终于成功了
    【解决方案2】:

    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
    

    【讨论】:

      【解决方案3】:
      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'
      )
      /
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-06
        • 1970-01-01
        • 1970-01-01
        • 2018-10-14
        相关资源
        最近更新 更多