此 SQL 查询中的 WHERE 子句有什么问题?

What is wrong with the WHERE clause in this SQL query?

select *
from tblProduct full join tblProductSales on tblProduct.id = tblProductSales.id
where tblProduct.id <> tblProductSales.id;

我修复了语法错误,但它仍然不会 运行。我不断收到以下错误:

unknown column 'tblProduct.id' in 'where clause'

注意 table 'tblProduct'

中有一列 'id'

当您在 SQL 中使用大写字母时,对 table 名称和列都使用引号。尝试:

select * from "tblProduct" 
full join "tblProductSales" on "tblProduct".id = "tblProductSales".id
where "tblProduct".id <> "tblProductSales".id;

MySql不支持full join,报错不描述问题。

MySql 中完全联接的一种解决方法是将两个查询联合到 return,所有查询都从左侧开始,有间隙,所有从右侧开始,有间隙。

create table tblProduct (id integer);
create table tblProductSales (id integer);
INSERT INTO tblProduct VALUES (1),(2),(3),(8),(9),(9);

INSERT INTO tblProductSales VALUES (1),(1),(2),(3),(4),(5);


SELECT * FROM tblProduct P
LEFT JOIN tblProductSales S ON P.id = S.id
UNION ALL
SELECT * FROM tblProduct P
RIGHT JOIN tblProductSales S ON P.id = S.id
WHERE P.id IS NULL
✓

✓

  id |   id
---: | ---:
   1 |    1
   1 |    1
   2 |    2
   3 |    3
   8 | null
   9 | null
   9 | null
null |    4
null |    5

db<>fiddle here