【发布时间】:2019-11-20 15:16:22
【问题描述】:
我在 Laravel-6.x 中构建了一个查询,但得到了重复的条目。
public function get()
{
$catArr = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45'];
$platformArr = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15'];
$regionArr = ['1', '2', '3', '4', '5', '6', '7'];
$languageArr = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26'];
return Product::join('category_product', 'category_product.product_id', '=', 'products.id')
->join('product_region', 'product_region.product_id', '=', 'products.id')
->join('language_product', 'language_product.product_id', '=', 'products.id')
->whereIn('category_product.category_id', $catArr)
->whereIn('products.platform_id', $platformArr)
->whereIn('product_region.region_id', $regionArr)
->whereIn('language_product.language_id', $languageArr)
->groupBy('products.id')
->paginate(18);
}
这是一个简单的 SQL 语句
SELECT * from `products` inner join `category_product` on `category_product`.`product_id` = `products`.`id` inner join `product_region` on `product_region`.`product_id` = `products`.`id` inner join `language_product` on `language_product`.`product_id` = `products`.`id` where `category_product`.`category_id` IN (1,2,3,4,5,6,7,8,9) and `products`.`platform_id` IN (10) and `product_region`.`region_id` IN (1,2,3,4,5,6,7) and `language_product`.`language_id` IN (26) LIMIT 20
我想我必须添加 GROUP BY。但是 HeidiSQL 返回一个错误:
Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'shopdb.products.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
有什么办法吗?
【问题讨论】:
-
你试过
SELECT DISTINCT(*)吗? -
或者只是改变你的
sql_mode
标签: sql laravel percona heidisql