【问题标题】:How can I update a column (multiple rows) in a table with the lower value between 2 column in 2 tables?如何在 2 个表中的 2 列之间更新表中的列(多行)?
【发布时间】:2015-07-14 06:49:59
【问题描述】:

我需要比较卖家的佣金和产品的佣金,并把较低的值详细放在佣金一栏中。

表1:详情 doc 佣金卖家产品 1 0 001 0101 1 0 001 0315 1 0 001 0425 2 0 001 0389 2 0 001 0315 表2:产品 代码描述委员会 0101 产品 1 5 0315 产品 2 7 0425 产品 3 3 0389 产品 4 8 . . . 表 3:卖家 代号委员会 001 卖家 1 6 002 卖家 2 3 . . .

我这样做了:

declare @comisionVendedor float,@comisionProducto float, @comision float,@prod='0101'
set @comisionVendedor=0
set @comisionProducto=0
select @comisionVendedor=comision from vendedor join details on seller.CODe=details.seller where details.doc=1 and prod=@prod
select @comisionProducto=products.COMISION from products join details on products.CODe=details.PRODuct where details.doc=1 and prod=@prod
if @comisionVendedor>@comisionProducto begin set @comision=@comisionProducto end
else begin set @comision=@comisionVendedor end

update details set comision=@comision where details.doc=1 and product=@prod

它工作正常,但只有一种产品。如何将3行的comision列替换为文档编号1?

【问题讨论】:

  • 它们都有不同的产品。因此您的代码按预期工作。尝试删除@prod 的硬编码并改用JOIN

标签: sql select multiple-tables


【解决方案1】:

您可以使用printf 命令

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    相关资源
    最近更新 更多