首页 > 解决方案 > SQL查询使用C#返回太多相同的列数据

问题描述

这是该部分:

    string sql = "select col1, product_id, col3, col4, col[n], col100 from inventory where store_code = '" + sStore + "' order by product_id";

    OracleCommand cmd = new OracleCommand(sql, conn);
    cmd.CommandType = CommandType.Text;
    OracleDataReader dr = cmd.ExecuteReader();

    string sColl = "";
    while (dr.Read()) {
       if (dr["product_id"].ToString() != sColl) {
          if (dr["product_id"].ToString() != " " && dr["product_id"].ToString() != "") {
              ...
              Console.WriteLine(dr["product_id"].ToString()) //checking 
              [operational code]
              ...
          }
       }
       sColl = dr["product_id"].ToString();
    }

在达到后续不同的 product_id 之前,我笨拙地尝试使操作代码(靠近中间的位置)在同一 product_id 的重复级联中的每个不同 product_id只工作一次。问题是,当然,我必须等待很长时间才能完成例程。幸运的是,对于每个唯一的 product_id,操作代码只使用一次,但我必须等待例程遍历一百万个其他相同的product_id。

换句话说,目前,在 [操作代码] 所在的输出中可视化,半垂直英里的重复、相同的产品 ID“0002000038”当前正被转储到 Console.WriteLine(dr["product_id"].ToString( )) 在它更改为另一个顶点之前。半英里的“0066133890”,然后持续几分钟,直到完成大约 10 个 product_id。

我真正需要的是: 我希望有一种方法可以重写上面的代码,而不是:

0002000038
0002000038
0002000038
0002000038
0002000038
0002000038
0002000038
...[half a mile more of this]
0002000038
0066133890
0066133890
0066133890
0066133890
0066133890
0066133890
0066133890
...[half a mile more of this]
0066133890
...

输出将是:

0002000038
0066133890
...

标签: c#sqlselect

解决方案


如果product_id是您正在使用的唯一列,则无需查询其他字段。完成此操作后,distinct关键字应该可以解决问题:

SELECT   DISTINCT product_id
FROM     inventory
WHERE    store_code = /* something */
ORDER BY product_id

推荐阅读