asp.net中的嵌套SQL查询

时间:2012-01-02 16:14:05

标签: asp.net sql sql-server nested-queries

我必须运行1个查询,然后运行第一个查询的结果。我意识到我可能只是将第一个结果集抛出一个数组然后遍历数组,但有没有办法只是嵌套查询,这样我就不必乱七八糟了?

这是我目前的代码:

    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 (item_sale_id=@item_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 (tblBidder.bidder_sale_id=@item_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 (tblBidHistory.bid_item_id=@item_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 (B.bidder_sale_id=@item_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 (I.item_sale_id=@item_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中创建嵌套查询。

2 个答案:

答案 0 :(得分:2)

尝试:

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 i.item_sale_id=@item_sale_id) v
where rn=1

答案 1 :(得分:1)

我非常怀疑以下是正确的:太多的假设和对问题缺乏理解:但这是加入他们的一种方式 假设商品通过Bid_Item_ID加入BH 为I.Item_lot添加了顺序并添加了项目数据 添加项目数据以进行选择和分组。

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 (B.bidder_sale_id=@item_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 (I.item_sale_id=@item_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 (BH.bid_item_id=@item_id) AND (BH.bid_type = '2') 
ORDER BY I.Item_Lot, BH.bid_price DESC, BH.bid_date DESC