Unknown column in subquery where clause(子查询 where 子句中的未知列)
问题描述
我的 INNER JOIN 子查询的 where 子句有问题.我收到 M.idMembre 的未知列错误.我尝试使用表名而不是别名,但我遇到了同样的问题.我还尝试从子查询中删除 WHERE 子句,并在子查询之后的 ON 子句中添加此条件.但是,无论哪种方式,我都有同样的问题.我觉得这很明显我在这里失踪了.
I'm having a problem in the where clause of my INNER JOIN subquery. I'm receiving a unknown column error for M.idMembre. I've tried using the table name instead of the alias but I get the same issue. I've also tried removing the WHERE clause from the subquery and adding this condition in the ON clause after the subquery. However, I'm having the same issue either way. I feel it's something obvious I'm missing here.
SELECT DISTINCT M.`idMembre` , `couponsTypes`.`maxCouponType`
FROM membres AS `M`
INNER JOIN (
SELECT idMembre, MAX( coupons.`idType` ) AS `maxCouponType`
FROM coupons
WHERE coupons.`idMembre` = M.`idMembre`
GROUP BY idMembre
) AS `couponsTypes`
ON M.`idMembre` = couponsTypes.`idMembre`
ORDER BY maxCouponType DESC
如果您需要更多信息,请告诉我.
Let me know if you need more information.
推荐答案
不允许在连接子句的子查询中引用外部表.解决此问题的一种方法是根据连接条件在子查询中执行 group by:
You are not allowed to reference outer tables in a subquery in a join clause. One way to solve this is by doing a group by in the subquery based on the join condition:
SELECT DISTINCT M.`idMembre`, `couponsTypes`.`maxCouponType`
FROM membres AS `M`
INNER JOIN
(SELECT idMembre, MAX(coupons.`idType`) AS `maxCouponType`
FROM coupons
GROUP BY idmembre
) `couponsTypes`
ON couponstypes.idMembre = M.idMember
ORDER BY maxCouponType DESC
但是,您根本不需要 membres 表.虽然在外层select中引用,但相当于coupons type表中的member id.因此,您可以将查询编写为:
But, you don't need the membres table at all. Although referenced in the outer select, it is equivalent to the member id in the coupons type table. So, you can write your query as:
SELECT idMembre, MAX(coupons.`idType`) AS `maxCouponType`
FROM coupons
GROUP BY idmembre
ORDER BY 2 DESC
这可能是最简单、最有效的表述方式了.
This is probably the simplest and most efficient way formulation.
这篇关于子查询 where 子句中的未知列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:子查询 where 子句中的未知列
基础教程推荐
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 在多列上分布任意行 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
