使用Count(*)函数INNER JOIN有3个表

时间:2015-12-04 02:37:46

标签: sql-server visual-studio count inner-join

我在使用count函数时让内部联接工作有问题,因为它说'Count函数需要至少1个参数'。下面是我在aspx.cs文件中的代码。感谢提前帮助:)

[INNER加入问题代码] [1]

 protected void CalculateNums_Click(object sender, EventArgs e)
{
    String connectionString = WebConfigurationManager.ConnectionStrings["UniString"].ConnectionString;
    SqlConnection myConnection = new SqlConnection(connectionString);

    String CIT = "1";

    myConnection.Open();

    String findCIT = "SELECT COUNT(*) FROM (SELECT COUNT(class.class_id, class.pathway_year_id, classlist.classlist_id, classlist.class_id, classlist.user_id,pathway_years.id, pathway_years.pathway_id, pathways.id, pathways.pathway) AS findCITString FROM class INNER JOIN classlist ON classlist.class_id = class.class_id INNER JOIN pathway_years ON pathway_years.id = class.pathway_year_id INNER JOIN pathways ON pathways.id = pathway_years.pathway_id WHERE pathways.id = '" + CIT+"')t";


    SqlCommand findCITCommand = new SqlCommand(findCIT, myConnection);

    Int32 findCITString = Convert.ToInt32(findCITCommand.ExecuteScalar().ToString());
    myConnection.Close();

    numInCIT.Text = Convert.ToString(findCITString);
}

ErrorAfterDistinct

String findCIT = "SELECT COUNT(*) FROM (SELECT DISTINCT class.class_id, class.pathway_year_id, classlist.classlist_id, classlist.class_id,      classlist.user_id, pathway_years.id, pathway_years.pathway_id, pathways.id, pathways.pathway FROM class INNER JOIN classlist ON classlist.class_id = class.class_id INNER JOIN pathway_years ON pathway_years.id = class.pathway_year_id INNER JOIN pathways ON pathways.id = pathway_years.pathway_id WHERE pathways.id = '" + CIT + "') as t";

1 个答案:

答案 0 :(得分:0)

你没有使用MySQL而是使用MSSQL:"计数功能需要至少1个参数"是MSSQL错误消息。

使用MSSQL,您只需为COUNT提供一个参数。以下是您可能正在寻找的查询:

SELECT
  COUNT(*)
  FROM 
    (SELECT 
       DISTINCT class.class_id,
                class.pathway_year_id,
                classlist.classlist_id,
                classlist.class_id,
                classlist.user_id,
                pathway_years.id,
                pathway_years.pathway_id,
                pathways.id,
                pathways.pathway
    FROM class
    INNER JOIN classlist ON classlist.class_id = class.class_id 
    INNER JOIN pathway_years ON pathway_years.id = class.pathway_year_id 
    INNER JOIN pathways ON pathways.id = pathway_years.pathway_id 
    WHERE pathways.id = '" + CIT+"') as t

请注意在内部查询中使用DISTINCT而不是COUNT()

修改:

很难理解您要对此查询执行的操作。无论如何,它仍然无法工作,因为内部查询中的某些列名称被使用了两次,如class_id

以下是我将如何重写它:

SELECT
  COUNT(*)
FROM class
INNER JOIN classlist ON classlist.class_id = class.class_id
INNER JOIN pathway_years ON pathway_years.id = class.pathway_year_id
INNER JOIN pathways ON pathways.id = pathway_years.pathway_id
WHERE
  pathways.id = '" + CIT + "'
GROUP BY
  class.class_id,
  class.pathway_year_id,
  classlist.classlist_id,
  classlist.class_id,
  classlist.user_id,
  pathway_years.id,
  pathway_years.pathway_id,
  pathways.id,
  pathways.pathway