【问题标题】:count with IF condition in SQL在 SQL 中使用 IF 条件计数
【发布时间】:2016-10-16 07:37:11
【问题描述】:

我将如何添加IF condition,如果为单个学生插入了五个科目数据,那么我想将其限制为六个数据....正如我在下面的查询中所做的那样,但我无法添加 @ 987654322@这个。

SELECT Count(Student_ID) as 'StudentCount' 
FROM tbCourseSemOne
where Student_ID=1 
Having Count(Student_ID) < 6 and Count(Student_ID) > 0;

【问题讨论】:

  • 添加简单的输入和输出以便更好地理解
  • 这是什么意思:如果为单个学生插入了 5 个 学科数据,那么我想限制 6 个 数据?
  • 您要排除超过 5 个条目的学生,还是只包括每个学生 5 个条目?一些示例数据和输入/输出会有所帮助..
  • 输入输出有多简单@shamimreza
  • 你应该添加你的样本数据和你想要的预期结果(这就是简单的输入和输出的意思)。因此我们可以知道您希望将哪些数据限制为结果的逻辑。

标签: sql sql-server sql-server-2008 sql-server-2005 sql-server-2008-r2


【解决方案1】:
               Solved myself :)
               string sql = "SELECT Count(Student_ID) FROM CourseSemOne where Student_ID='"+Student_ID+"' Group by Student_ID ";
                SqlConnection con = new SqlConnection(@"Data Source=LOCALHOST\SQLEXPRESS;Initial Catalog=CS_DB;Integrated Security=True");
                con.Open();
                SqlCommand cmd = new SqlCommand(sql, con);
                int count = Convert.ToInt32(cmd.ExecuteScalar());
                con.Close();

                if (count <5 && count >=0)
                {
                    obj.insertSemCourseOne(Student_ID, Course_Code, Course_Title, Total_Marks, Obtain_Marks, Grade, Value, Cr_Hours, Grade_Point, GPA, CGPA);
                   // DatabaseConnnectionClass.UserMessage("Added");
                    MessageBox.Show("Added Subject");
                    Dataloaddd();
                }
                else
                {

                    MessageBox.Show("Sorry!For this Student ID = "+Student_ID+" You cannot add more than 5 data.");
                }

【讨论】:

    【解决方案2】:
    Declare @Counter int 
    Set @Counter=(SELECT Count(Student_ID) as 'StudentCount' FROM CourseSemOne 
                  where Student_ID=1 Having Count(Student_ID) < 6 and Count(Student_ID) > 0)
    if(@Counter <6)
    print'Sorry! You cannot add more than five subject data for a single stduent'
    else
    print'Insert Code'
    

    【讨论】:

      【解决方案3】:
      Declare @Counter int 
      Set @Counter=(SELECT isnull(Count(Student_ID),0) as 'StudentCount' FROM     CourseSemOne  where Student_ID=1 )
      if(@Counter >5)
      print'Sorry! You cannot add more than five subject data for a   single         stduent'
      else
      
      print'Insert Code'
      

      【讨论】:

        【解决方案4】:

        我认为您正在搜索的是 IF NOT EXISTS,如果您的查询返回多行,它将返回 FALSE

        If NOT Exist(Your Query)
        Begin
          //Your insert query
        End
        

        更新:

        IF Exists (SELECT 1  FROM CourseSemOne where Student_ID=1 Having Count(Student_ID) > 5)
        BEGIN
            Print 'Your Error Message'
        END
        

        【讨论】:

        • Declare @Counter int Set @Counter=(SELECT Count(Student_ID) as 'StudentCount' FROM CourseSemOne where Student_ID=1 有 Count(Student_ID) 0) if(@计数器
        • 一个月后我自己解决了这个逻辑
        【解决方案5】:
        Declare @Counter int 
        Set @Counter=(SELECT Count(Student_ID) as 'StudentCount' FROM CourseSemOne 
        

        其中 Student_ID=1 具有 Count(Student_ID) 0) 如果(@Counter

        【讨论】:

          【解决方案6】:

          如果我理解正确,您希望防止每个学生在表中插入超过 5 个科目。

          这通常通过语句后触发器来完成。这种触发器不会在每一行之后触发,而只会在完成命令之后触发。然后,您可以计算条目并在条目过多时引发错误。

          但是,据我所知,SQL Server 不提供语句后触发器。所以我认为这在 SQL Server 中是不可能的。 (当然,我可能错了。)

          【讨论】:

          • 你得到它的解决方案应该是什么?@Thorsten Kettner
          • 如前所述,我想不出任何方法可以让它在 SQL Server 中完全自动运行。也许您可以在表中添加一个列counter,并使用约束counter in(1,2,3,4,5)(student_id, counter) 上的唯一键。因此,您只能为每个学生插入五个科目,但您必须在插入时找到所需的counter
          • 这是问题,先生@Thorsten Kettner
          猜你喜欢
          • 2012-04-05
          • 1970-01-01
          • 2016-01-23
          • 2016-03-08
          • 2018-05-05
          • 2020-11-20
          • 2017-06-01
          • 2017-01-11
          相关资源
          最近更新 更多