Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.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中的另一个表对结果进行排序_Mysql_Sql_Sql Order By_Inner Join_Where Clause - Fatal编程技术网

如何根据mysql中的另一个表对结果进行排序

如何根据mysql中的另一个表对结果进行排序,mysql,sql,sql-order-by,inner-join,where-clause,Mysql,Sql,Sql Order By,Inner Join,Where Clause,我想选择产品,以便根据另一个表选择最畅销的商品 select product_id from wp_product_details_fields where field in ( 'brand', 'model') group by product_id having ( max(`field` = 'brand' and `value` = 'Intel') = 1 OR

我想选择产品,以便根据另一个表选择最畅销的商品

select product_id
       from wp_product_details_fields
       where field in ( 'brand', 'model')
       group by product_id
       having  (  max(`field` = 'brand' and `value` = 'Intel') = 1 
                  OR
                  max(`field` = 'model' and `value` = 'Core i7') = 1  ) 
       order by (SELECT COUNT(*) AS magnitude 
                        FROM wp_order_items 
                        GROUP BY product_id 
                        ORDER BY magnitude DESC)
我有一个错误:

MySQL说:子查询返回超过1行

查询:

SELECT product_id
FROM (your complex query w/o ORDER BY) wpd
Left JOIN wp_order_items USING (product_id)
GROUP BY product_id
ORDER BY COUNT(*) /* DESC */

为了切中要害,我简化了查询,但我的原始查询是我刚刚编辑的。请再次检查谢谢,唯一的问题是此查询仅对wp_order_items表中的产品进行排序。我只是想按最畅销的商品分类,而不是删除它们。即使wp_order_items表中存在行,我如何获取行?我发现我应该使用left Join,谢谢您的帮助。子查询中只能有1行。看到你们的桌子结构也很好