比如我写了一个表:ID     姓名     书名
1      小王   《胜利之光》
2      小李   《失败卡卡》
3      大王   《哦哦啊啊》
4      大王   《胜利之光》假设这样的记录很多。现在我想知道,《胜利之光》一总有几本? 小王有几本书?其中小王的《胜利之光》有几本?求.net  C#  的写法。
麻烦详细点哦。
做出来测试过的最好,麻烦发到 [email protected]  署名,给分。

解决方案 »

  1.   

    SELECT COUNT(*) FROM 表 WHERE 书名='《胜利之光》'
    SELECT DISTINCT COUNT(*) FROM 表 WHERE 姓名='小王'
    SELECT COUNT(*) WHERE 书名='《胜利之光》' AND   姓名='小王'
      

  2.   

    SELECT DISTINCT COUNT(书名) FROM 表 -- 返回3
    SELECT COUNT(书名) FROM 表 WHERE 书名 = "胜利之光" --返回 2
      

  3.   


            public static int GetCount()
            {
                string sql="SELECT COUNT(*) FROM 表 WHERE 书名='《胜利之光》'"
                int result = -1;
                using (SqlConnection conn = Connection)
                {
                    try
                    {
                        SqlCommand cmd = new SqlCommand(sql, conn);
                        cmd.Parameters.AddRange(values);
                        result = Convert.ToInt32(cmd.ExecuteScalar());
                        conn.Close();
                    }
                    catch (Exception e)
                    {
                        Logger.WriteLog(sql, e);
                    }            }            return result;
            }
      

  4.   

    public static int GetCount() 
            { 
                string sql="SELECT COUNT(*) FROM 表 WHERE 书名='《胜利之光》'" 
                int result = -1; 
                using (SqlConnection conn = Connection) 
                { 
                    try 
                    { 
                        SqlCommand cmd = new SqlCommand(sql, conn); 
                        
                        result = Convert.ToInt32(cmd.ExecuteScalar()); 
                        conn.Close(); 
                    } 
                    catch (Exception e) 
                    { 
                        Logger.WriteLog(sql, e); 
                    }             }             return result; 
            } 
      

  5.   


    create table #book
    (  ID int identity(1,1) primary key,
      姓名 varchar(20),
      书名 varchar(50)
    )
    insert into #book select '小王','《胜利之光》'
    insert into #book select '小李','《失败卡卡》'
    insert into #book select '大王','《哦哦啊啊》'
    insert into #book select '大王','《胜利之光》'
    /*
    假设这样的记录很多。现在我想知道,《胜利之光》一总有几本?
     小王有几本书?其中小王的《胜利之光》有几本? 
    */
    --(1)《胜利之光》一总有几本?
    select count(*) 数量 from #book where 书名='《胜利之光》'
    数量
    -----------
    2
    --(2)小王有几本书?
    select count(*) 数量 from #book where 姓名='小王'
    数量
    -----------
    1
    --(3)其中小王的《胜利之光》有几本? 
    select count(*) 数量 from #book where 姓名='小王' and 书名='《胜利之光》'
    数量
    -----------
    1
      

  6.   

    DataRow[] rows= table.Select("姓名='' and 书名=''");
    rows.Length就可以获得了或者用
    int count=table.compute("count('id')","姓名='' and 书名=''")
      

  7.   

    一条SQL的来啦create table #book
    (  ID int identity(1,1) primary key,
      姓名 varchar(20),
      书名 varchar(50)
    )
    insert into #book select '小王','《胜利之光》'
    insert into #book select '小李','《失败卡卡》'
    insert into #book select '大王','《哦哦啊啊》'
    insert into #book select '大王','《胜利之光》'
    insert into #book select '小王','《C#》'
    insert into #book select '小王','《Visual Basic》'
    insert into #book select '特别','《胜利之光》'
    insert into #book select '小王','《C++》'
    insert into #book select '小王','《FoxBase2.1》'select 胜利之光总数=(select count(ID) from #book  as a where a.书名=c.书名)
    ,小王书的总数=(select count(ID) from #book as b where b.姓名=c.姓名)
    ,小王的胜利之光总数=(select count(ID) from #book as b where b.姓名=c.姓名 and b.书名=c.书名)
    from #book as c
    where c.书名='《胜利之光》' and c.姓名='小王'--查询结果
    胜利之光总数    小王书的总数   小王的胜利之光总数
    3             5         1
      

  8.   

    select (select count(*) from Tb where  书名='胜利之光'),
    (select count(*) from Tb where  姓名='小王'),
    (select count(*) from Tb where  姓名='小王' and 书名='胜利之光')
    from Tb
    在C#通过数据库操作类查询数据