热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

基于3个表更新列?-UpdatingaColumnbasedon3tables?

Ihave3tablessuchas我有3张桌子IndentHeader:IndentHeader:IndentIDStatusID--------

I have 3 tables such as

我有3张桌子

IndentHeader:

IndentHeader:

 IndentID        StatusID 
--------         ------   
    1             5      
    2             5

IndentDetail:

IndentDetail:

IndentID         ItemID           ItemStatusID
--------         ------           ------------
 1                22                 4
 1                23                 4
 2                11                 4
 2                12                 3
 2                13                 3

POIndent:

POIndent:

       POID             IndentID     ItemID      ItemStatusID
      --------           ------      ------      ------------
         1                1            22            4
         1                1            23            4
         1                2            11            4

I want to Update IndentHeader table StatusID = 4 when all the Items in the IndentDetail table (based on the IndentID) ItemstatusID becomes 4 otherwise I want to Update IndentHeader StatusID = 3. In the Condition I need to give POID. Based on the POID, the corresponding Indent is considered for both the IndentHeader and IndentDetail table. My desired Result should be like this:

当IndentHeader表格中的所有项(基于IndentID)都变成4时,我想更新IndentHeader表格中的小雕像sid = 4,否则我想更新IndentHeader小雕像sid = 3。在这种情况下,我需要给POID。基于POID,对IndentHeader和IndentDetail表都考虑了相应的缩进。我期望的结果应该是这样的:

IndentHeader:

IndentHeader:

 IndentID        StatusID 
--------         ------   
    1             4      
    2             3

How to achieve this? Please help me.

如何实现呢?请帮助我。

Hi all, this is my update command. But it update both the StatusID in IndentHeader as 4.

大家好,这是我的更新命令。但是它同时更新了IndentHeader中的小雕像4。

   UPDATE STR_IndentHeader
            SET StatusID = IID
            FROM
            (SELECT 
            STR_IndentDetail.IndentID, MIN(ItemStatusID) AS 'IID'
            FROM 
            STR_IndentDetail INNER JOIN PUR_POIndent PP
            ON PP.IndentID = STR_IndentDetail.IndentID
            AND PP.ItemID = STR_IndentDetail.ItemID
                        WHERE ItemStatusID = 4 AND PP.POID = 1
            GROUP BY STR_IndentDetail.IndentID) ID 
            WHERE ID.IndentID = STR_IndentHeader.IndentID 

I need all your valuable contributions. please help me...

我需要你所有的宝贵贡献。请帮我…

2 个解决方案

#1


3  

My [revised] solution use one ALL subquery to check ItemStatusID condition:

我的[修改过的]解决方案使用一个全子查询来检查itemcontrollsid条件:

DECLARE @MyPOID INT = 1;

DECLARE @IndentHeader TABLE
(
    IndentID INT PRIMARY KEY
    ,StatusID INT NOT NULL
);
INSERT  @IndentHeader 
VALUES  (1,5);
INSERT  @IndentHeader 
VALUES  (2,5);
INSERT  @IndentHeader 
VALUES  (3,5);

DECLARE @IndentDetail TABLE
(
    IndentID INT NOT NULL
    ,ItemID INT NOT NULL
    ,ItemStatusID INT NOT NULL
    ,PRIMARY KEY(IndentID, ItemID)
);
INSERT  @IndentDetail
VALUES  (1,22,4);
INSERT  @IndentDetail
VALUES  (1,23,4);
INSERT  @IndentDetail
VALUES  (2,11,4);
INSERT  @IndentDetail
VALUES  (2,12,3);
INSERT  @IndentDetail
VALUES  (2,13,3);
INSERT  @IndentDetail
VALUES  (3,22,3);

DECLARE @POIndent TABLE
(
    POID INT
    ,IndentID INT NOT NULL
    ,ItemID INT NOT NULL
    ,ItemStatusID INT NOT NULL
);
INSERT  @POIndent 
VALUES  (1,1,22,4);
INSERT  @POIndent 
VALUES  (1,1,23,4);
INSERT  @POIndent 
VALUES  (1,2,11,4);
INSERT  @POIndent 
VALUES  (2,3,22,4);

SELECT  *
FROM    @IndentHeader h;
SELECT  *
FROM    @IndentDetail d;
SELECT  *
FROM    @POIndent po;

UPDATE  @IndentHeader 
SET     StatusID = CASE WHEN 4 = ALL(SELECT d.ItemStatusID FROM @IndentDetail d WHERE d.IndentID = h.IndentID) THEN 4 ELSE 3 END
FROM    @IndentHeader h
WHERE   h.IndentID IN (SELECT po.IndentID FROM @POIndent po WHERE po.POID = @MyPOID);

SELECT  *
FROM    @IndentHeader h;

#2


2  

The gist of it is to

它的要旨是

  • find the minimum ItemStatusID for each IndentID
  • 找到每个IndentID最小的itemsid。
  • Join this back with IndentHeader
  • 加入IndentHeader
  • Use these in an UPDATE FROM statement
  • 在FROM语句的更新中使用这些

SQL Statement

UPDATE  IndentHeader
SET     StatusID = ihd.ItemStatusID
FROM    IndentHeader ih
        INNER JOIN (
            SELECT  ItemStatusID = MIN(id.ItemStatusID)
                    , ih.IndentID
            FROM    IndentHeader ih
                    INNER JOIN IndentDetail id ON id.IndentID = ih.IndentID
                    INNER JOIN POIndent pi ON pi.IndentID = id.IndentID
            WHERE   pi.POID = 1     
            GROUP BY
                    ih.IndentID     
        ) ihd ON ihd.IndentID = ih.IndentID         

Test script

;WITH IndentHeader (IndentID, StatusID) AS (
    SELECT 1, 5      
    UNION ALL SELECT 2, 5
)
, IndentDetail (IndentID, ItemID, ItemStatusID) AS (
    SELECT 1, 22, 4
    UNION ALL SELECT 1, 23, 4
    UNION ALL SELECT 2, 11, 4
    UNION ALL SELECT 2, 12, 3
    UNION ALL SELECT 2, 13, 3
)
, POIndent (POID, IndentID, ItemID, ItemStatusID) AS (
    SELECT 1, 1, 22, 4
    UNION ALL SELECT 1, 1, 23, 4
    UNION ALL SELECT 1, 2, 11, 4
)
--UPDATE    IndentHeader
--SET       StatusID = ihd.ItemStatusID
SELECT  ih.IndentID, ihd.ItemStatusID
FROM    IndentHeader ih
        INNER JOIN (
            SELECT  ItemStatusID = MIN(id.ItemStatusID)
                    , ih.IndentID
            FROM    IndentHeader ih
                    INNER JOIN IndentDetail id ON id.IndentID = ih.IndentID
                    INNER JOIN POIndent pi ON pi.IndentID = id.IndentID
            WHERE   pi.POID = 1     
            GROUP BY
                    ih.IndentID     
        ) ihd ON ihd.IndentID = ih.IndentID         

推荐阅读
author-avatar
宇智臣风
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有