Laravel雄辩的查询有两次输入,按分组不起作用

时间:2019-11-20 15:16:22

标签: sql laravel percona heidisql

我在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

enter image description here

任何解决方案吗?

1 个答案:

答案 0 :(得分:1)

因为这是关于您的mysql中的var builder = new StreamsBuilder(); // Our state store var storeBuilder = Stores.keyValueStoreBuilder( Stores.persistentKeyValueStore("SensorStates"), Serdes.String(), storeSerde); // Register the store builder builder.addStateStore(storeBuilder); builder.stream("input-topic", Consumed.with(Serdes.String(), inputSerde)) .transformValues(DurationProcessor::new, DurationProcessor.SENSOR_STATES) .to("result-topic", Produced.with(Serdes.String(), resultSerde)); var topology = builder.build(); 的问题

关于sql_mode=only_full_group_by

的模式很少

尝试更改您的sql_mode

本地设置

sql_mode

设置为全局

set sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

或更改set @@global.sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'; 文件:

在下面的my.cnf[mysqld]下添加此行

[mysql]

然后重新启动mysql。

PS:您的代码还有另一个问题。不要从所有这些表中选择所有字段,这会花费时间,并且最后一个表的SET sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'; 将覆盖先前的ID。选择所需的字段,如果名称重复,请使用id来命名其他名称

AS

使用ANY_VALUE

或者您可以在列中使用any_value:

        ->groupBy('products.id')
        ->selectRaw('products.*, category_product.id AS cp_id')
        ->paginate(18);

禁用严格模式

或者您可以将严格模式禁用为config / database.php

        ->groupBy('products.id')
        ->selectRaw('ANY_VALUE(products.id),..., category_product.id AS cp_id')
        ->paginate(18);