string sql_phy250 = "select count(*) from user_status where userID=(select userID from user_base where username ='" + name + "') and year='" + year + "' and month='" + month + "' and status =250 ";
        string sql_phy500 = "select count(*) from user_status where userID=(select userID from user_base where username ='" + name + "') and year='" + year + "' and month='" + month + "' and status =500 ";
        string sql_phy750 = "select count(*) from user_status where userID=(select userID from user_base where username ='" + name + "') and year='" + year + "' and month='" + month + "' and status =750 ";
        string sql_phy1000 = "select count(*) from user_status where userID=(select userID from user_base where username ='" + name + "') and year='" + year + "' and month='" + month + "' and status =1000 ";
从数据表user_status里面分别查询status等于250 500 750 1000的记录条数!
能合并成一条语句嘛?
这样要执行4次数据库查询好痛苦。
SQL高手指点下!!!谢谢!

解决方案 »

  1.   

    string sql_phy_ALL = "select count(*) from user_status where userID=(select userID from user_base where username ='" + name + "') and year='" + year + "' and month='" + month + "' and (status =250 or status =500 or status =750 or status =1000 ";
      

  2.   

    差个右括号:
    string sql_phy_ALL = "select count(*) from user_status where userID=(select userID from user_base where username ='" + name + "') and year='" + year + "' and month='" + month + "' and (status =250 or status =500 or status =750 or status =1000 )";
      

  3.   

    --查总数?
    string sql_phy250 = "select count(*) from user_status where userID=(select userID from user_base where username ='" + name + "') and year='" + year + "' and month='" + month + "' and status in (250,500,750,1000) ";--按照status分别查?
    string sql_phy250 = "select status , count(*) from user_status where userID=(select userID from user_base where username ='" + name + "') and year='" + year + "' and month='" + month + "' and status in (250,500,750,1000) group by status";
      

  4.   

    [SQL]
    string sql_phy250 = "select status , count(*) 
    from user_status 
    where userID=(select userID from user_base where username ='" + name + "') 
    and year='" + year + "' and month='" + month + "' 
    and status in (250,500,750,1000) group by status";
    [/SQL]
      

  5.   

    string sql = "select status , count(*)  
    from user_status  
    where userID=(select userID from user_base where username ='" + name + "')  
    and year='" + year + "' and month='" + month + "'  
    //1种方法 and status in (250,500,750,1000)";//2种方法 and (status =250 or status =500 or status =750 or status =1000 )";
      

  6.   

    除了 in和OR以外 
    还有UNION ALL
      

  7.   

    string sql_phy250 = "select count(*) from user_status where userID=(select userID from user_base where username ='" + name + "') and year='" + year + "' and month='" + month + "' and status =250 
    union all
    select count(*) from user_status where userID=(select userID from user_base where username ='" + name + "') and year='" + year + "' and month='" + month + "' and status =500 
    union all 
    select count(*) from user_status where userID=(select userID from user_base where username ='" + name + "') and year='" + year + "' and month='" + month + "' and status =750 
    union all
    select count(*) from user_status where userID=(select userID from user_base where username ='" + name + "') and year='" + year + "' and month='" + month + "' and status =1000 "