我想根據左表連接這兩個表並匹配行值。右表不一定包含左表中的所有ITEMNUM
。並不是每個VNDITNUM
都會在左邊的表中有一個ITEMNUM
。使用sql連接兩個表
LEFT TABLE (559 rows)
--Filtered based on column IVCOGSIX = 137
SELECT
ITEMNMBR
FROM dbo.IV00101
WHERE IVCOGSIX = 137
+---------+
| ITEMNUM |
+---------+
| Item 1 |
| Item 2 |
| Item 3 |
| Item 4 |
+---------+
RIGHT TABLE (553 rows)
--Filtered based on column VENDORID = 90505EP
SELECT
ITEMNMBR,
VNDITNUM
FROM dbo.IV00103
WHERE VENDORID = '90505EP'
+---------+-------------+
| ITEMNUM | VNDITNUM |
+---------+-------------+
| Item 1 | VendorNum 1 |
| Item 2 | VendorNum 2 |
| Item 4 | VendorNum 4 |
| Item X | VendorNum 5 |
+---------+-------------+
,我想獲得的輸出是這樣的 -
JOINED TABLE (559 rows)
+---------+-------------+
| ITEMNUM | VNDITNUM |
+---------+-------------+
| Item 1 | VendorNum 1 |
| Item 2 | VendorNum 2 |
| Item 3 | NULL |
| Item 4 | VendorNum 4 |
+---------+-------------+
然而,我的查詢一直給我的只有548行,我也不一定確保它留下什麼行了。
這裏是我當前的查詢 -
SELECT
cogs.ITEMNMBR,
vin.VNDITNUM
FROM METRO.dbo.IV00101 cogs
LEFT JOIN METRO.dbo.IV00103 vin
on vin.ITEMNMBR = cogs.ITEMNMBR
WHERE
cogs.IVCOGSIX = 137
AND vin.VENDORID = '90505EP'
我如何加入這兩個表,以獲得559行左表和右表中的所有匹配的項目?
它是SQL-Server還是MySQL?數據庫名稱'dbo'通常是SQL-Server,那麼爲什麼你有'mysql'標籤? – Barmar
@Barmar SQL-Server。對不起,出於某種原因,我正在考慮SQL-Server的語言是MySql – Adjit