在asp.net中嵌套的SQL查询

在asp.net中嵌套的SQL查询

问题描述:

我必须运行1个查询,然后用第一个结果运行另一个查询。我意识到我可能只是抛出数组中的第一个结果集,然后遍历数组,但有没有一种方法来嵌套查询,以便我不必乱搞数组?在asp.net中嵌套的SQL查询

这里是我当前的代码:

SqlConnection conn2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["wesdb1SQL"].ToString()); 
    SqlCommand strSQL2; 
    SqlDataReader itemReader2; 

    using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["wesdb1SQL"].ToString())) 
    using (SqlCommand strSQL = conn.CreateCommand()) 
    { 
     strSQL.CommandText = "SELECT item_id,item_lot,item_title,item_est_lo,item_est_hi,item_timed_start,item_reserve FROM tblItem WHERE ([email protected]_sale_id) ORDER BY item_lot"; 

     strSQL.Parameters.Add(new SqlParameter("@item_sale_id", SqlDbType.VarChar, 10, ParameterDirection.Input, true, 0, 0, "item_sale_id", DataRowVersion.Current, itemSaleId1_Var)); 

     try 
     { 
      conn.Open(); 
      using (SqlDataReader itemReader = strSQL.ExecuteReader()) 
      { 
       if (itemReader.Read()) 
       { 
        using (conn2) 
        using (strSQL2 = conn2.CreateCommand()) 
        { 
         strSQL2.CommandText = "SELECT TOP (1) MAX(tblBidHistory.bid_price) AS bid_price, tblMailList.mail_Email1, tblBidHistory.bid_bidder_id, tblMailList.mail_FirstName, tblMailList.mail_LastName, tblBidHistory.bid_item_id FROM tblBidHistory INNER JOIN tblBidder ON tblBidHistory.bid_bidder_id = tblBidder.bidder_number AND ([email protected]_sale_id) INNER JOIN tblMailList ON tblBidder.bidder_mail_id = tblMailList.mail_ID GROUP BY tblMailList.mail_Email1, tblBidHistory.bid_bidder_id, tblMailList.mail_FirstName, tblMailList.mail_LastName, tblBidHistory.bid_item_id,tblBidHistory.bid_type,tblBidHistory.bid_date HAVING ([email protected]_id) AND (tblBidHistory.bid_type = '2') ORDER BY bid_price DESC,bid_date DESC"; 

         strSQL2.Parameters.Add(new SqlParameter("@item_sale_id", SqlDbType.VarChar, 10, ParameterDirection.Input, true, 0, 0, "item_sale_id", DataRowVersion.Current, itemSaleId1_Var)); 
         strSQL2.Parameters.Add(new SqlParameter("@item_id", SqlDbType.VarChar, 10, ParameterDirection.Input, true, 0, 0, "item_id", DataRowVersion.Current, itemReader["item_id"].ToString())); 

         try 
         { 
          conn2.Open(); 
          using (itemReader2 = strSQL2.ExecuteReader()) 
          { 
           if (itemReader2.Read()) 
           { 
            if (count % 2 == 0) 
            { 
             results_Var += "<tr><td colspan=\"2\"><font size=\"2\">" + itemReader["item_lot"].ToString() + " - <u>" + itemReader["item_title"].ToString() + "</u></font></td><td><font size=\"2\">$" + itemReader["item_est_lo"].ToString() + " - $" + itemReader["item_est_hi"].ToString() + "</font></td><td>" + itemReader2["bid_price"].ToString() + " - " + itemReader2["bid_bidder_id"].ToString() + " (" + itemReader2["mail_FirstName"].ToString() + " " + itemReader2["mail_LastName"].ToString() + ")</td><td><font size=\"2\">$" + itemReader2["bid_price"].ToString() + "</font></td></tr>"; 
            } 
            else 
            { 
             results_Var += "<tr><td colspan=\"2\" bgcolor=\"#b0e0e6\"><font size=\"2\">" + itemReader["item_lot"].ToString() + " - <u>" + itemReader["item_title"].ToString() + "</u></font></td><td><font size=\"2\">$" + itemReader["item_est_lo"].ToString() + " - $" + itemReader["item_est_hi"].ToString() + "</font></td><td>" + itemReader2["bid_price"].ToString() + " - " + itemReader2["bid_bidder_id"].ToString() + " (" + itemReader2["mail_FirstName"].ToString() + " " + itemReader2["mail_LastName"].ToString() + ")</td><td><font size=\"2\">$" + itemReader2["bid_price"].ToString() + "</font></td></tr>"; 
            } 
           } 
           itemReader2.Close(); 
          } 
         } 
         catch (Exception e1) 
         { 
          throw new Exception(e1.Message); 
         } 
         finally 
         { 
          conn2.Close(); 
         } 
        } 
       } 
       itemReader.Close(); 
      } 
     } 
     catch (Exception e2) 
     { 
      throw new Exception(e2.Message); 
     } 
     finally 
     { 
      conn.Close(); 
     } 
    } 

两个查询的输出用于创建多行数据的表。每行都是数据库中的一个项目,出价者的信息最高,包括出价本身以及一些类别信息。类别信息是第一个查询,而唯一项目信息是第二个查询(在这里您可以看到使用两个结果集构建的表格行)。

编辑

我改变xQbert查询一点,结束了:

SELECT MAX(BH.bid_price) AS bid_price, ML.mail_Email1, BH.bid_bidder_id, ML.mail_FirstName, ML.mail_LastName, BH.bid_item_id, I.item_lot, I.item_title, I.item_est_lo, I.item_est_hi, I.item_timed_start, I.item_reserve 
FROM tblBidHistory BH 
INNER JOIN tblBidder B ON BH.bid_bidder_id = B.bidder_number AND ([email protected]_sale_id) 
INNER JOIN tblMailList ML ON B.bidder_mail_id = ML.mail_ID 
INNER JOIN tblItem I ON I.Item_ID = BH.Bid_item_id 
WHERE ([email protected]_sale_id) And (BH.bid_type = '2') 
GROUP BY ML.mail_Email1, BH.bid_bidder_id, ML.mail_FirstName, ML.mail_LastName, BH.bid_item_id, BH.bid_type, BH.bid_date, I.item_lot, I.item_title, I.item_est_lo, I.item_est_hi, I.item_timed_start, I.item_reserve 
ORDER BY I.Item_Lot 

这给我的数据,但它给了我所有的投标人对每个项目,而不是为每个出价最高项目。我不确定是否需要进行不同的分组,也可能使用子查询。

此外,要做到这一点,我只是作出1查询。但是,我仍然想知道如何在SQL中进行嵌套查询。

+0

正在使用的是何种数据库? – 2012-01-02 16:30:47

+0

SQL。如果你想要的东西,我不知道。抱歉。我将如何发现? – James 2012-01-02 16:37:04

+0

看到一些样本预期输出真的很有用。 MS-SQL(2005/2008等),MySQL,Oracle,DB2,PostGres, – xQbert 2012-01-02 16:41:03

尝试:

select * from 
(SELECT i.item_id, 
     i.item_lot, 
     i.item_title, 
     i.item_est_lo, 
     i.item_est_hi, 
     i.item_timed_start, 
     i.item_reserve, 
     h.bid_price, 
     l.mail_Email1, 
     h.bid_bidder_id, 
     l.mail_FirstName, 
     l.mail_LastName, 
     h.bid_item_id, 
     row_number() over (partition by i.item_id 
          order by h.bid_price DESC, h.bid_date DESC) rn 
FROM tblItem i 
INNER JOIN tblBidHistory h 
     on i.item_id = h.bid_item_id=i.item_id AND h.bid_type = '2' 
INNER JOIN tblBidder b 
     ON h.bid_bidder_id = b.bidder_number AND b.bidder_sale_id=i.item_sale_id 
INNER JOIN tblMailList l 
     ON b.bidder_mail_id = l.mail_ID 
WHERE [email protected]_sale_id) v 
where rn=1 
+0

我试着将row_number()函数(直到并包括rn的整行)添加到我的select语句的末尾,然后将其全部包含在另一个select语句中,其中rn = 1并且接收到错误> ORDER BY子句在视图,内联函数,派生表,子查询和公用表表达式中是无效的,除非还指定了TOP或FOR XML。 我把我的order by子句外移到第一个select语句中,并按v.column命令,然后开始收到错误,说tblItem。item_id(和其他)不在group by或aggregate函数中,因此无法选择。 – James 2012-01-02 18:32:36

+0

@詹姆斯,你有没有在这个答案中试过查询? – 2012-01-02 19:29:21

+0

当我使用整个查询时,出现错误'附近的语法不正确'('.' – James 2012-01-02 19:42:01

我很怀疑下面是正确的:太多的假设和缺乏问题的认识:但是这是一个方式加入他们 假设项目加入由I.Item_lot并通过Bid_Item_ID 新增为BH添加的项目数据 添加项目数据以选择和分组。

SELECT TOP (1) MAX(BH.bid_price) AS bid_price, ML.mail_Email1, BH.bid_bidder_id, 
    ML.mail_FirstName, ML.mail_LastName, BH.bid_item_id, I.item_lot, I.item_title, 
    I.item_est_lo, I.item_est_hi, I.item_timed_start, I.item_reserve 
FROM tblBidHistory BH 
INNER JOIN tblBidder B 
    ON BH.bid_bidder_id = B.bidder_number 
    AND ([email protected]_sale_id) 
INNER JOIN tblMailList ML 
    ON B.bidder_mail_id = ML.mail_ID 
INNER JOIN tblItem I 
    ON I.Item_ID = BH.Bid_item_id 
WHERE ([email protected]_sale_id) 
GROUP BY ML.mail_Email1, BH.bid_bidder_id, 
    ML.mail_FirstName, ML.mail_LastName, BH.bid_item_id, BH.bid_type, BH.bid_date, 
    I.item_lot, I.item_title, I.item_est_lo, I.item_est_hi, I.item_timed_start, 
    I.item_reserve 
HAVING ([email protected]_id) AND (BH.bid_type = '2') 
ORDER BY I.Item_Lot, BH.bid_price DESC, BH.bid_date DESC 
+0

我认为OP希望出售中每件商品的最高出价,而不是出售中任何商品的最高最高出价。 – 2012-01-02 17:35:24

+0

@MarkBannister是的,没错。我试图在相同的销售中获得每个单独的项目。 – James 2012-01-02 17:37:32

+0

并且在稍微的tweeks之后,我得到了所有的数据,但是在某些情况下我得到了多行相同的数据。 – James 2012-01-02 17:38:53