0

我正在做一个项目,我的目标是使学校的收费系统自动化。我正在 VB.NET 2012 中实现该项目并使用 SQLServerCE 处理数据。

我的情况是这样的:

我有两个表,即:“feecollection”和“StudentDetails”

表feecollection的列如下收费

同样,StudentDetails表的列如下学生详情

现在我想找出违约学生的姓名。我正在根据名称、月份和默认字段进行过滤。

逻辑:给定月份收费的学生的姓名已支付,因此默认为所有在 StudentDetails 中但不在 feecollection 中的姓名,其 defaulter 字段为“1”(1 表示他们已付款)。

我的查询:

select StudentDetails.Name where StudentDetails.Name not in
 (Select feecollection.Name, feecollection.Month, feecollection.defaulter, 
   StudentDetails.Name from feecollection 
   inner join StudentDetails on feecollection.Name = StudentDetails.Name 
   where StudentDetails.Name = 'def' and feecollection.month = 'January' 
   and feecollection.defaulter = '1'

但是这个查询不能正常工作。我在哪里做错了?

4

1 回答 1

0

假设 defaulter 字段为“0”或学生未付款时缺少 feecollection 记录,我将使用类似这样的 SQL 语句。

SELECT StudentDetails.Name FROM StudentDetails 
    LEFT JOIN feecollection ON StudentDetails.Name = feecollection.Name 
    AND feecollection.Payment_For_Month = 'Jan' 
    WHERE feecollection.defaulter = '0' OR feecollection.defaulter IS NULL;

这样,您应该在 StudentDetails 中获取匹配的费用收集记录在默认值中具有“0”或不存在的所有记录。

编辑

如果您仍想使用 IN 运算符,则应使用此语句:

SELECT StudentDetails.Name FROM StudentDetails WHERE
    StudentDetails.Name NOT IN 
    (SELECT feecollection.Name FROM feecollection 
    WHERE feecollection.Payment_For_Month = 'Feb' 
    AND feecollection.defaulter = '1')

我已经在一个简化的数据集上对此进行了测试:

学生资料:

Name    Class    Section        
------------------------
Adam    101      Sect1
Bob     101      Sect
Charlie 101      Sect2

收费:

Name    Payment_For_Month    defaulter
--------------------------------------
Adam    Jan                  1
Adam    Feb                  1
Bob     Jan                  1

有了这些数据,上面的语句只返回 Charlie,而第二条语句返回 Bob 和 Charlie。

于 2013-11-04T14:41:15.403 回答