【问题标题】:What is wrong with the WHERE clause in this SQL query?此 SQL 查询中的 WHERE 子句有什么问题?
【发布时间】:2022-01-18 02:43:01
【问题描述】:
select *
from tblProduct full join tblProductSales on tblProduct.id = tblProductSales.id
where tblProduct.id <> tblProductSales.id;

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

“where 子句”中的未知列“tblProduct.id”

请注意,表 'tblProduct' 中有一个列 'id'

【问题讨论】:

  • 奇数。但无论如何你的查询毫无意义。
  • 展示tblProduct表的结构
  • 您修复了哪些语法错误? MySql 甚至不支持全连接。

标签: mysql sql join where-clause


【解决方案1】:

MySql 不支持全连接,错误没有描述问题。

MySql 中完全连接的一种解决方法是合并两个查询,以从左侧返回所有有间隙的查询,并从右侧返回所有有间隙的查询。

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 ---: | ---: 1 | 1 1 | 1 2 | 2 3 | 3 8 | 9 | 9 | | 4 | 5

db小提琴here

【讨论】:

  • 如果你改为INSERT INTO tblProductSales VALUES (1),(1),(2),(2),(3),(4),(5);会发生什么
  • 顺便说一句,p left join s 与 s right join p 相同。
  • @jarlh - 我颠倒了表格,仍在考虑您的第一条评论。
  • 你必须做 UNION ALL。
  • 并确保 RIGHT JOIN 不返回任何常见行(即 P 行)。
【解决方案2】:

当您在 SQL 中使用大写字母时,请为表名和列使用引号。试试:

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

【讨论】:

  • 您的建议是否记录在任何地方?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
相关资源
最近更新 更多