简体   繁体   English

使用两个表中的数据更新临时表

[英]updating a temp table with data from two tables

I have created a temp table (@temptable) where i left a column blank because I will fill it with another select statement. 我创建了一个临时表(@temptable),其中我将列留空,因为我将用另一个select语句填充它。

I have a table that has 5 rows called books: 我有一个有5行叫做book的表:

 BookNum | BookDesc | BookDate | ......
---------|----------|----------|--------
 00      | A sto... | 6/6/2013 | ......
 00      | {null}   | {null}   | ......
 02      | The t... | 6/6/2013 | ......
 00      | {null}   | 6/6/2013 | ......
 02      | {null}   | 6/6/2013 | ......

and my temptable has a column that includes the BookNum: 我的temptable有一个包含BookNum的列:

...... | total_books | title | BookCode | CountOfBook
-------|-------------|-------|----------|-------------
...... |      4      | Once  |    00    |   {null}
...... |      4      | Once  |    00    |   {null}
...... |      4      | Once  |    00    |   {null}
...... |      4      | Once  |    00    |   {null}
...... |      4      | Once  |    00    |   {null}
...... |      4      | Once  |    00    |   {null}
...... |      4      | Four  |    02    |   {null}
...... |      4      | Four  |    02    |   {null}

And what I want to do is get the count of how many books I have of a specific book from the books table where the date is not null, and put it into the CountOfBook column for my @temptable, but I can't seem to figure it out. 而我想要做的是从书籍表中获取一本特定书籍的书数,其中日期不为空,并将其放入我的@temptable的CountOfBook列中,但我似乎无法想办法。 it should look like this: 它应该是这样的:

...... | total_books | title | BookCode | CountOfBook
-------|-------------|-------|----------|-------------
...... |      4      | Once  |    00    |     2
...... |      4      | Once  |    00    |     2
...... |      4      | Once  |    00    |     2
...... |      4      | Once  |    00    |     2
...... |      4      | Once  |    00    |     2
...... |      4      | Once  |    00    |     2
...... |      4      | Four  |    02    |     2
...... |      4      | Four  |    02    |     2

as there are 2 of each book where the date is not null. 因为每本书中有2本日期不为空。

Try this , You can use subquery in update statement - which will help you- 试试这个, 你可以在update语句中使用子查询 - 这将有助于你 -

   update tempTable t
    set CountOfBook = (select count(*) as CountOfBook_2 
                        from Books 
                       where BookData is not null
                       Group by BookCode 
                       having BookCode=t.BookCode );

sqlfiddle example -> using oracle 11 g => fiddle sqlfiddle示例 - >使用oracle 11 g => 小提琴

Update: try using this: link referred 更新:尝试使用此: 链接引用

   update tempTable 
    set CountOfBook = (select count(*) as CountOfBook_2 
                        from Books 
                       where BookData is not null
                       Group by BookCode 
                       having BookCode=t.BookCode)
    FROM tempTable as t;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM