c# - SQL查询使用C#返回太多相同的列数据

标签 c# sql select

这是部分:

    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_ids。

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

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
...

最佳答案

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

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

关于c# - SQL查询使用C#返回太多相同的列数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51225706/

相关文章:

sql - 选择其他列的值相等的对

php - 选择具有不同放置可能组合的字段值

mysql - 从同一列中选择多个值

c# 使用 EPPLUS 使工作表名称只读?

c# - 在列表框中的项目之前添加字符串

c# - 如何实现通用 GetById() 其中 Id 可以是各种类型

sql - 两张表的列相加出错 : SQL Server 2005

php - MySql缓存会导致性能问题吗?

c# - 获取以偏移量开头的数组

mysql - 优化 SELECT 查询,知道我们正在处理一个有限的范围