MS-SQL给出了错误的结果 [英] MS-SQL giving wrong results

查看:117
本文介绍了MS-SQL给出了错误的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张桌子,一张是销售,一张是购买。在销售中,我只卖了一件商品。我买了那个项目两次。当我试图通过查询查看该项目的销售和购买时,该查询显示错误的结果。



我的查询是

I am having two tables one is sales and one is purchase. In sales I sold one item one time only. I purchased that item two times. When I tried to see that item sales and purchases with a query , that query showing wrong results.

my query is "

select salesDB.sales,purchDB.purchase from salesDB  inner join purchDB  on salesDB.id=purchDB.id where salesDB.id=1





结果如下所示

销售额购买

5000 10000

5000 15000



实际有一次只有我卖了。价值5000 / - 。我买了两次。价值25000

所以我错了。我创建了与salesDB.id(主键)和purchaseDB.id的关系



我尝试过的事情:



我搜索了解决方案,并从该网站找到了一些解决方案还有其他网站。我尝试过但结果相同。



results are like below
SALES PURCHASE
5000 10000
5000 15000

There actually one time only I sold. Value 5000/-. Two times I purchased. Value 25000
so where I did mistake. I created relation to salesDB.id(primary key) and purchaseDB.id

What I have tried:

I googled for solution and found some solutions from this site and other sites also. I tried those but same result.

推荐答案

没有错误 - 你的查询显示了销售和购买的所有内容。

这些文章可能会帮助您理解

SQL连接的可视化表示 [ ^ ]

在SQL中连接表 [ ^ ]



您可能想要的是UNION或类似的东西
There is no mistake - your query is showing everything from Sales AND Purchases.
These articles might help you understand
Visual Representation of SQL Joins[^]
Joining Tables in SQL[^]

What you probably want is a UNION or something like this
select salesDB.id, 'SALE' as tranType, sales
from salesDB
UNION
select purchDB.id, 'PURC' as tranType, purchase
from purchDB

这应该给

1 SALE 5000
1 PURC 10000
1 PURC 15000


这篇关于MS-SQL给出了错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆