使用WHERE

时间:2017-08-18 08:07:23

标签: sql sql-server select where

我希望UPDATE表格中的一列,但在每一行中,必须在另一行的WHERE上依赖另一个值。

这就是表格的样子。

BusinessUnitGUID | ClassName | DefaultGUID

    5        | PriceList | 349FDAFD34M
    5        | Footer1   | 987IOXG376L
    5        | Header1   | 12WQX954MIO
    7        | PriceList | NULL
    7        | Footer1   | NULL
    7        | Header1   | NULL

结果应该是这样。

BusinessUnitGUID | ClassName | DefaultGUID

    5        | PriceList | 349FDAFD34M
    5        | Footer1   | 987IOXG376L
    5        | Header1   | 12WQX954MIO
    7        | PriceList | 349FDAFD34M
    7        | Footer1   | 987IOXG376L
    7        | Header1   | 12WQX954MIO

但是这个显示的查询不起作用,因为它返回很多行,所以它不准确。

update cSC_BusinessUnit
set defaultguid =
    (
    select defaultguid
    from cSC_BusinessUnit 
    where BusinessUnitGUID = 5
    )
where BusinessUnitGUID = 7

6 个答案:

答案 0 :(得分:4)

您还需要检查ClassName:

update b1
set b1.defaultguid =
    (
    select b2.defaultguid
    from cSC_BusinessUnit b2
    where b2.BusinessUnitGUID = 5
        AND b2.ClassName = b1.ClassName
    )
from cSC_BusinessUnit b1
where b1.BusinessUnitGUID = 7

答案 1 :(得分:2)

使用以下短版本

private void fetchData(DataSnapshot dataSnapshot) {
    Marks value = dataSnapshot.getValue(Marks.class);
    Log.v("Marks Fragment", "" +value);
    findPercentage(value);
    // Use an iterator.
    Iterator<Marks> ite = mMarksList.iterator();
    while(ite.hasNext()) {
        Marks iteValue = ite.next();
        if(iteValue.equals(value)) ite.remove();
    }
    mMarksList.add(value);

    ....
}

答案 2 :(得分:0)

这会解决您的问题吗?

update cSC_BusinessUnit t1
set defaultguid =
    (
    select defaultguid
    from cSC_BusinessUnit t2
    where t2.BusinessUnitGUID = 5
    and t1.classname = t2.classname
    )
where BusinessUnitGUID = 7

答案 3 :(得分:0)

您也可以动态设置所有NULL列,只要您只有一个 ClassName 出现

update A
set DefaultGUID =
    (
        select B.DefaultGUID
        from cSC_BusinessUnit B
        where B.ClassName = A.ClassName
        And B.DefaultGUID IS NOT NULL
    )
from cSC_BusinessUnit A

答案 4 :(得分:0)

您可以尝试

update cSC_BusinessUnit a
set a.defaultguid =
    (
    select defaultguid
    from cSC_BusinessUnit b
    where b.BusinessUnitGUID = 5 and b.ClassName = a.ClassName
    )
where a.BusinessUnitGUID = 7

答案 5 :(得分:0)

更新b1 设置b1.defaultguid =     (     选择b2.defaultguid     来自cSC_BusinessUnit b2     其中b2.BusinessUnitGUID = 5         AND b2.ClassName = b1.ClassName     ) 来自cSC_BusinessUnit b1 其中b1.BusinessUnitGUID = 7