问题

SELECT

cat.CategoryID as CategoryID,

count(p.ProductID) as CountProducts

FROM

Category as cat

LEFT JOIN Products as p on p.CategoryID IN

(SELECT CategoryID FROM Category as cd

WHERE cd.ParrentCategoryID = '876')

WHERE

CategoryID = '876'

ORDER by Name

We are get error - "Column 'CategoryID' in where clause is ambiguous".

Tell me plese how will be right?

回答1:

The error you get tells you that the column CategoryID in your WHERE clause is ambiguous, that means that the system has a problem to identify the appropriate column because there are multiple CategoryID columns.

In order to fix this problem, use the alias to specify which column you want to use for your WHERE clause:

SELECT cat2.CategoryID AS CategoryID

,cat2.Name AS CategoryName

,COUNT(p.ProductID) AS CountProducts

FROM Category AS cat

INNER JOIN Category AS cat2 ON cat2.ParrentCategoryID = cat.CategoryID

INNER JOIN Products AS p ON p.CategoryID = cat2.CategoryID

WHERE cat.CategoryID = '876'

GROUP BY cat2.CategoryID, cat2.Name

ORDER BY cat2.Name

I also changed a bit the query to get the same result but instead of using a combination of LEFT JOIN + IN clause + sub query, i used INNER JOIN clauses.

With this query you only need to define your desired CategoryID once and it will automatically get every child categories.

I'm not sure that your query runs correctly because you're using the COUNT function without grouping the results by CategoryID...

Hope this will help you.

回答2:

The "ambiguous column" error means that there's a reference to a column identifier, and MySQL has two (or more) possible columns that match the specification.

In this specific case, it's the reference to CategoryID column in the WHERE clause.

It's "ambiguous" whether CategoryID is meant to refer to the CategoryID column from cat, or refer to the CategoryID column from p.

The fix is to qualify the column reference with the table alias to remove the ambiguity. That is, in place of CategoryID, specify either cat.CategoryID or p.CategoryID.

Some additional notes:

It's very odd to have an IN (subquery) predicate in the ON clause. If CategoryID is not guaranteed to be unique in the Category table, this query would likely generate more rows than would be expected.

The normal pattern for a query like this would be something akin to:

SELECT cat.CategoryID AS CategoryID

, COUNT(p.ProductID) AS CountProducts

FROM Category cat

LEFT

JOIN Products p

ON p.CategoryID = cat.CategoryID

WHERE cat.ParrentCategoryID = '876'

ORDER BY cat.Name

Normally, the join predicates in the ON clause reference columns in two (or more) tables in the FROM clause. (That's not a SQL requirement. That's just the usual pattern.)

The predicate in the ON clause of the query above specifies that the value of the CategoryID column from the cat (Category) table match the value in the CategoryID column from the p (Product) table.

Also, best practice is to qualify all columns referenced in a query with the name of the table, or a table alias, even if the column names are not ambiguous. For example, the Name column in the ORDER BY clause.

One big benefit of this is that if (at some time in the future) a column named Name is added to the Products table, the query with an unqualified reference to Name would (then) begin to throw an "ambiguous column" error for the that reference. So, qualifying all column names avoids a working query "breaking" when new columns are added to existing tables.

Another big benefit is that to the reader of the statement. MySQL can very quickly look in the dictionary, to find which table Name is in. But readers of the SQL statement (like myself) who do not "know" which table contains the column named Name, would also need to look at the table definitions to find out. If the name were qualified, we would already "know" which table that column was from.

回答3:

Try WHERE cat.CategoryID = '876'

来源:https://stackoverflow.com/questions/25678777/mysql-in-where-clause-is-ambiguous

mysql where clause is ambiguous_mysql …in where clause is ambiguous相关推荐

  1. mysql查询报错: ORDER BY clause is not in GROUP BY..this is incompatible with sql_mode=only_full_group_by

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 我的情况 : Mysql 5.7.21 版本运行sql 报错如题,同样的 sql 直接本地运行不报错 ...

  2. 【MySQL】ERROR 1055 (42000) ROUP BY clause this is incompatible with sql_mode=only_full_group_by

    1.背景 我本地mysql 8执行了一个SQL报错 ysql> select * from tb_emp_bigdata group by id%10 limit 150000; ERROR 1 ...

  3. MySQL 1054错误 Unknown column .... in 'on clause'

    提示: ERROR 1054 (42S22): Unknown column ... in 'on clause' 原因: MySQL5.0 Bug, 要把联合的表用括号包含起来才行: 例: Sql代 ...

  4. MySQL错误:Column ‘pno‘ in field list is ambiguous是什么问题呢?

    ERROR 1052 (23000): Column 'pno' in field list is ambiguous 很多初学者在MySQL的基础学习中会出现上述的报错. 问题出在没有对 'pno' ...

  5. OGG-01008 Extract displays Discarding bad record (discard recs=1) when using filter or where clause

    因为在extract參数文件里使用了where语句,而where后面的的条件列又不是主键,没有为update.delete操作记录日志,因此会报1008错误. Applies to: Oracle G ...

  6. 开源语义理解框架 Clause API 文档:快速实现聊天机器人

    在上一篇文章<基于开源语义理解框架 Clause 实现聊天机器人 > 中,很多读者关心如何使用 Clause,在 Clause Wiki 文档中心 中也有很多指导使用的资料,现将 API ...

  7. 【SA8295P 源码分析】36 - MDIO Clause 22、Clause 45 条款介绍

    [SA8295P 源码分析]36 - MDIO Clause 22.Clause 45 条款介绍 1. Clause 22 条款通信协议 2. Clause 45 条款通信协议 3. 通过 Claus ...

  8. 我应该如何处理MySQL中的--secure-file-priv?

    本文翻译自:How should I tackle --secure-file-priv in MySQL? I am learning MySQL and tried using a LOAD DA ...

  9. mysql测试spring事务是否生效

    同时对三张表进行插入操作,事务保证完整性.下面进行简单测试: 1. 锁定表 锁定用户表 LOCK TABLES user WRITE; 查看表是否锁定: show OPEN TABLES where ...

  10. mysql 表设计时的update_time自动更新

    11.3.5 Automatic Initialization and Updating for TIMESTAMP and DATETIME 原文地址:https://dev.mysql.com/d ...

最新文章

  1. 机器学习和计算机视觉有关的数学
  2. Mysql取得分组的前n名
  3. php上传文件到七牛云源码,laravel上传文件到七牛云存储
  4. html文字斜体变成正体,$$中的字母如何由斜体变成正体?
  5. 服务器系统装软路由,服务器系统设置软路由
  6. 轻松查看Internet Explorer缓存文件
  7. 第一次Sprint总结
  8. ubuntu阿里云快速下载
  9. linux中哪一个标记可以作为子进程,linux系统编程试卷(答案)
  10. testbench的简单例子和模板
  11. Oracle P6培训系列:12进度计算
  12. 关于阿拉qq大盗屏蔽winhex之后的破解.
  13. Unity3D物理渲染算法研究【PBR】
  14. 黑龙江高中计算机结业水平测试,黑龙江省普通高中信息技术学业水平考试标准.doc...
  15. 解决Ubuntu18.04和Win10双系统系统时间不对的问题
  16. 推荐9个能让你看一天的网站
  17. 举例说明国内云计算厂商代表有哪些?
  18. SLAM高翔视频第二讲萝卜习题
  19. Podometer计步器
  20. java 输入出生年月_java代码实现输入你出生年月日,计算到今天已经度过了多少天...

热门文章

  1. 医学病理图像:细胞间质与间质细胞的区别
  2. Android WebApp NativeApp 适配测试自动化平台GoGo实现 东海陈光剑
  3. 上下相机贴合对位计算公式_相机界的小公主康泰时Contax g1
  4. java base64转图片打不开_解决通过 Base64 解码得到的图片无法打开查看的问题
  5. aiohttp+aiofiles异步爬虫光速下载图片
  6. go用smpt包发送邮件, 被抄送收不到邮件bug
  7. 鸿蒙处理器985相当于内核多少,麒麟985处理器相当于骁龙多少_麒麟985处理器性能测评...
  8. [POJ3537]Crosses and Crosses
  9. 全国计算机软件专业二本大学排名,2017全国二本大学专业排名
  10. Mybatis-plus最新代码生成器(3.5.1+)的使用