c# - 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
...
解决方案
如果product_id
是您正在使用的唯一列,则无需查询其他字段。完成此操作后,distinct
关键字应该可以解决问题:
SELECT DISTINCT product_id
FROM inventory
WHERE store_code = /* something */
ORDER BY product_id
推荐阅读
- mysql - 如何以非root用户身份运行mysql?
- azure - 每个实例的 Web 套接字和 IP 连接有什么区别?
- javascript - Discord.js v12 提到作为机器人前缀
- javascript - 如何在 *ngFor 中的数组元素之间放置逗号?
- javascript - 带路由器的可滑动 Vuetify 选项卡
- flutter - 如何正确构建 Flutter 设计
- django - 初级问题 Django 3.0:基于类的视图
- xamarin.forms - 糟糕的榜单表现
- python - Python - 在每个 X 插入表后添加空闲时间(使用 API)
- laravel - 多个where子句,Laravel