mysql - 内连接语法错误

标签 mysql sql

我在 MySQL 上的表

table 供应商

---------------------------------------------------
SupplierID |    SupplierName |  ContactName Address
---------------------------------------------------

表格产品

-----------------------------------------------
ProductID | ProductName |SupplierID Unit| Price
-----------------------------------------------

查询

SELECT Suppliers.SupplierName
       , ProductName
       , Price
FROM Products 
INNER JOIN (SELECT SupplierID
                   , MAX(Price) AS maxPrice
            FROM Products
            GROUP BY SupplierID) as gp ON Products.Price = gp.maxPrice
INNER JOIN Suppliers ON Suppliers.SupplierID = Products.SupplierID;

我想要做的是根据每个供应商最昂贵产品的价格获取产品名称、供应商名称及其各自的价格(这就是子查询正在提取的内容),但其余的查询不起作用,不知道为什么

SQL错误

Syntax error (missing operator) in query expression 'Products.Price =
gp.maxPrice INNER JOIN Suppliers ON Suppliers.SupplierID =
Products.SupplierID'.

最佳答案

SELECT Suppliers.SupplierName,ProductName,Price FROM (Products 
INNER JOIN (SELECT SupplierID,  MAX(Price) AS maxPrice FROM Products GROUP BY SupplierID) as gp ON Products.Price = gp.maxPrice)
INNER JOIN Suppliers ON Suppliers.SupplierID = Products.SupplierID;

有效果吗?

关于mysql - 内连接语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32019388/

相关文章:

mysql - 从具有相同 id 的两个查询中获取分组记录

java - 如何在jpql中选择列表元素的属性

sql - Postgres windows 函数与聚合组依据

sql - Oracle 在解析 prepared statement 时是否选择了默认的执行计划?

mysql - 需要评论页面数据库设计方面的帮助

PHP 表可链接列

php - 重新创建/找到 Joomla/Community Builder 'Latest Users' 模块。 MySQL/Joomla 中的日期

mysql - 为什么两个不同的 mysql 函数在对从单个 mysql_query() 检索的数据执行时返回不同的行?

MySQL 'NOT NULL' 错误

sql - 从另一个表插入数百万行