SQL 加入问题并显示错误值
SQL Join Issues and displaying wrong value
我确信这个问题已经得到解答,但作为 SQL 的新手,我什至不确定到底要寻找什么。
我希望根据唯一 ID 和持续时间加入两个表,以 return 定价和两个表之间的定价不匹配。这是示例:
Table1
PK | Item | Duration | Price
1 | 1 | 10 | 1.99
1 | 1 | 15 | 2.99
Table2
PK | Item | Duration | Price
1 | 1 | 10 | 1.99
1 | 1 | 15 | 3.99
现在这是我的查询:
SELECT table1.item,
table1.duration,
table1.pice,
table2.item,
table2.duration,
table2.pice,
FROM table1
INNER JOIN table2 ON table1.item = TABLE 2 item
WHERE table1.duration = table2.duration
我的结果有问题。
它匹配项目和持续时间,但价格 return 错误的价格(它似乎是拉它找到的第一个价格)即它 returns:
1 | 10 | 1.99 | 1 | 10 | 1.99
1 | 15 | 1.99 | 1 | 15 | 3.99
我预计与 15 持续时间相关的第二个价格为 return 2.99。但它是 returning 1.99。
感谢任何帮助或指导。
我更正了您代码中的拼写错误。
select table1.item, table1.duration, table1.price,
table2.item, table2.duration, table2.price
from table1 inner
join table2
on table1.item = table2.item
where table1.duration = table2.duration;
MSAccess 给出了您期望的结果,而不是您得到的结果。
考虑仔细检查您的代码。或者,也许您的 SQL 实施方式不同。
这是 TSQL(SQL 服务器)中的一个示例,用于额外的信用。
SELECT
a.[item]
,a.[duration]
,a.[price]
,b.[item]
,b.[duration]
,b.[price]
,a.[price] - b.[price] AS [difference] --Difference between two values.
,CASE WHEN a.[price] - b.[price] <> 0.00 THEN 'NOT MATCHED' --When difference exists.
WHEN a.[price] - b.[price] = 0.00 THEN 'MATCHED' --When difference not exists.
ELSE NULL END AS [matched] --When records not exists.
FROM table1 a
INNER JOIN table2 b ON a.[item] = b.[item]
AND a.[duration] = b.[duration]
结果:
item duration price item duration price Difference Matched
1 10 1.99 1 10 1.99 0.00 MATCHED
1 15 2.99 1 15 3.99 -1.00 NOT MATCHED
那如果你只想拉取不匹配的记录,那么在最下面加上:
WHERE a.[price] - b.[price] <> 0.00
我确信这个问题已经得到解答,但作为 SQL 的新手,我什至不确定到底要寻找什么。
我希望根据唯一 ID 和持续时间加入两个表,以 return 定价和两个表之间的定价不匹配。这是示例:
Table1
PK | Item | Duration | Price
1 | 1 | 10 | 1.99
1 | 1 | 15 | 2.99
Table2
PK | Item | Duration | Price
1 | 1 | 10 | 1.99
1 | 1 | 15 | 3.99
现在这是我的查询:
SELECT table1.item,
table1.duration,
table1.pice,
table2.item,
table2.duration,
table2.pice,
FROM table1
INNER JOIN table2 ON table1.item = TABLE 2 item
WHERE table1.duration = table2.duration
我的结果有问题。
它匹配项目和持续时间,但价格 return 错误的价格(它似乎是拉它找到的第一个价格)即它 returns:
1 | 10 | 1.99 | 1 | 10 | 1.99
1 | 15 | 1.99 | 1 | 15 | 3.99
我预计与 15 持续时间相关的第二个价格为 return 2.99。但它是 returning 1.99。
感谢任何帮助或指导。
我更正了您代码中的拼写错误。
select table1.item, table1.duration, table1.price,
table2.item, table2.duration, table2.price
from table1 inner
join table2
on table1.item = table2.item
where table1.duration = table2.duration;
MSAccess 给出了您期望的结果,而不是您得到的结果。 考虑仔细检查您的代码。或者,也许您的 SQL 实施方式不同。
这是 TSQL(SQL 服务器)中的一个示例,用于额外的信用。
SELECT
a.[item]
,a.[duration]
,a.[price]
,b.[item]
,b.[duration]
,b.[price]
,a.[price] - b.[price] AS [difference] --Difference between two values.
,CASE WHEN a.[price] - b.[price] <> 0.00 THEN 'NOT MATCHED' --When difference exists.
WHEN a.[price] - b.[price] = 0.00 THEN 'MATCHED' --When difference not exists.
ELSE NULL END AS [matched] --When records not exists.
FROM table1 a
INNER JOIN table2 b ON a.[item] = b.[item]
AND a.[duration] = b.[duration]
结果:
item duration price item duration price Difference Matched
1 10 1.99 1 10 1.99 0.00 MATCHED
1 15 2.99 1 15 3.99 -1.00 NOT MATCHED
那如果你只想拉取不匹配的记录,那么在最下面加上:
WHERE a.[price] - b.[price] <> 0.00