Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在数据库中解决sql查询_Mysql_Sql - Fatal编程技术网

Mysql 在数据库中解决sql查询

Mysql 在数据库中解决sql查询,mysql,sql,Mysql,Sql,我已经 沃罗杜 M专业 你能帮我吗 也许是这样的? 没有DB无法测试,但我认为这应该很接近 select top 1 P.productId, SUM(S.Quantity), P.Description, P.Price from Products P INNER JOIN Sales S on P.ProductID=S.productId group by P.productId, P.Description, P.Price order by SUM(S.Quantity) desc

我已经

沃罗杜 M专业

你能帮我吗

也许是这样的? 没有DB无法测试,但我认为这应该很接近

select top 1 P.productId, SUM(S.Quantity), P.Description, P.Price
from Products P
INNER JOIN Sales S on P.ProductID=S.productId
group by P.productId, P.Description, P.Price
order by SUM(S.Quantity) desc
前1只给出一行,订单购买总额说明将使其成为数量最多的一行。
删除最上面的一个,以获得按总量降序排列的完整列表。

您的where子句不太正确,请尝试以下操作:

select *
from Products
where productsId = (
  select productId 
  from Sales 
  group by productId 
  order by sum(quantity) desc 
  limit 1);

正确格式化的SQL非常易于阅读和编写。请共享一些示例数据和所需结果。您的简短的表定义、5个单词的问题和非常错误的sql都不足以说明如何将它们组合在一起。例如,最畅销的产品意味着什么?Department表与此有什么关系?他使用的是MySql,因此它将是LIMIT 1而不是TOP 1。限制在最后,例如在WHERE或have之后。