本文介绍了从不同的表中减去并添加两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想从不同的表中减去两列。 表格书: BookID | BookName |作者|版| PublishingYear |副本|货架|行 1 | SQL |罗伯特| 3 | 2005 | 3 | A |第三个 表问题:(在此我创建了与tblPerson联接以显示PersonName而不是PersonID) BookID | BookName | DateIssue | ReturnDate | PersonName |复制 1 | SQL | 2015-10-12 | 2015-10-12 |约翰| 1 table返回: BookID | BookName | DateIssue | ReturnDate | PersonName |复制 1 | SQL | 2015-10-12 | 2015-10-12 |约翰| 1 Sql查询: 选择(tblBook.copies) - (tblIssue.copies) FROm tblBook FULL join tblIssue ON tblBook.copies = tblIssue.copies 此查询不会减去这两列(副本)。 我想在发行本书时从tblbook列副本(原始值)减去列(副本)tblIssue。 当我从tbl返回书时返回,它给了我tblBook中列(副本)的原始值。解决方案 Quote: ON tblBook.copies = tblIssue.copies 你确定吗?在一个通用的长手SQL我会做 选择( A.copies-B.copies)numCopies 来自 tblBook A,tblIssue B 其中 A.BookId = B.BookId; 注意BookId上的连接 - 我不明白你为什么要使用你正在使用的ON子句 - 所以会发生什么如果你使用 ON tblBook.BookId = tblIssue.BookId 而不是? 除了Garth J Lancaster [ ^ ]我建议你阅读: SQL连接的可视化表示 [ ^ ]。这篇 - 非常好的文章 - 可以帮助您了解联接的工作方式! I want to subract two columns from different table.table book:BookID | BookName | Author | Edition | PublishingYear | copies| Shelf | Row 1 | SQL | Robert | 3 | 2005 | 3 | A | thirdtable Issue: (in this I have created join with tblPerson to show the PersonName instead of PersonID)BookID | BookName | DateIssue | ReturnDate | PersonName | copies 1 | SQL | 2015-10-12 | 2015-10-12 | john | 1table Return:BookID | BookName | DateIssue | ReturnDate | PersonName | copies 1 | SQL | 2015-10-12 | 2015-10-12 | john | 1Sql Query:Select (tblBook.copies) - (tblIssue.copies)FROm tblBookFULL join tblIssueON tblBook.copies = tblIssue.copiesThis query doesn't subract these two columns(copies).I want to minus the column (copies) tblIssue from tblbook column copies(original value) when I issue the book.And when I return the book from tbl Return, it gives me the original value in the column(copies) in tblBook. 解决方案 Quote:ON tblBook.copies = tblIssue.copiesare you sure ? in a generic long hand SQL I'd doselect (A.copies-B.copies) numCopiesfrom tblBook A, tblIssue Bwhere A.BookId = B.BookId;notice the join on BookId - I dont see why you're using the ON clause you're using - so what happens if you use ON tblBook.BookId = tblIssue.BookIdinstead ?In addition to solution 1 by Garth J Lancaster[^] i'd suggest to read this: Visual Representation of SQL Joins[^]. This - very good article - may help you understand how joins work! 这篇关于从不同的表中减去并添加两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-27 16:26