[英]Extracting data from two databases by matching the value of the column
"I am writing a query to match data from one database table to another database table" “我正在编写一个查询以将数据从一个数据库表匹配到另一个数据库表”
Database 1.Table1: number CFG Name
A123 28A tom
B123 23C sindy
Database2.Table2: ITEM results
28A yes
23C no
If I search for Number like A123, it must match these columns CFG and ITEM and it brings up like CFG, ITEM, Results."如果我搜索像 A123 这样的数字,它必须匹配这些列 CFG 和 ITEM,它会出现 CFG、ITEM、Results 之类的结果。”
SELECT database1.table1.number, database1.table1.CFG, database2.table2.ITEM,database2.table2.results
FROM database1.table1, database2.table2
Where database1.table1.number ='A123'
AND database1.table1.CFG = database2.table2.ITEM
"I expect the output of like CFG ITEM results, it's displaying nothing" “我希望 output 类似于 CFG ITEM 结果,它什么也没显示”
I prepared your statement for testing, if this works for you replace declared tables with databaseN.tableN.我准备了你的测试语句,如果这对你用 databaseN.tableN 替换声明的表有用。
DECLARE @Database1Table1 TABLE
(number VARCHAR(10) NOT NULL PRIMARY KEY,
CFG VARCHAR(10) NOT NULL,
Name NVARCHAR(50) NULL)
DECLARE @Database2Table2 TABLE
(ITEM VARCHAR(10) NOT NULL PRIMARY KEY,
results BIT NOT NULL)
INSERT INTO @Database1Table1
(number,
CFG,
Name)
VALUES
('A123',
'28A',
N'tom'),
('B123',
'23C',
N'sindy'),
('C123',
'35X',
N'no matching sample')
INSERT INTO @Database2Table2
(ITEM,
results)
VALUES
('28A',
1),
('23C',
0)
SELECT d1.number,
d1.CFG,
d2.ITEM,
d2.results
FROM @Database1Table1 AS d1
INNER JOIN @Database2Table2 AS d2 ON d1.CFG = d2.ITEM
--WHERE d1.number = 'A123'
With Where clause you get:使用 Where 子句,您将获得:
number CFG ITEM results
A123 28A 28A 1
without:没有:
number CFG ITEM results
A123 28A 28A 1
B123 23C 23C 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.