MYSQL 500万行复杂查询

问题描述 投票:0回答:1

我有一个包含 500 万行的表。 示例

+----+-----------+---------+-------
|  product    | date       |  count    | 
+-------------+------------+-----------+
|  платье     | 04-01-2024 | 186574    | 
|  штаны      | 04-01-2024 | 20564     |
|  кепка      | 07-01-2024 | 104443    | 
|  штаны      | 06-01-2024 | 10574     | 
|  платье     | 06-01-2024 | 223001    |
+-------------+------------+-----------+

我需要选择具有 2 个自定义日期的行,然后如果产品 name1 = name2 获得计数之间的差异,然后获得此差异的前 100 名。

现在我创建 2 个选择,然后循环遍历数组来获取它。但我有关于记忆的错误。我如何在 mysql 中创建 1 个查询?

我的询问

    $get_ex = $connection->prepare("SELECT * FROM product WHERE date = :date");
    $get_ex->execute(array(':date' => $first_date));
    $ex_list_2 = $get_ex->fetchAll(PDO::FETCH_ASSOC);

    
    $get_ex_3 = $connection->prepare("SELECT * FROM product WHERE date = :date");
    $get_ex_3->execute(array(':date' => $second_date));
    $ex_list_3 = $get_ex_3->fetchAll(PDO::FETCH_ASSOC);

    
    $result = Array();
    $i=0;
    foreach($ex_list_2 as $key => $value)
    {
        foreach($ex_list_3 as $key2 => $value2)
        {
            if($value['product_name'] == $value2['product_name'])
            {
                $result[$i]['product_name'] = $value['product_name'];
                $result[$i]['count_1'] = $value['count'];
                $result[$i]['count_2'] = $value2['count'];
                $result[$i]['res'] = $value['count']- $value2['count'];
                $i++;
            }
        }
        
    }

和结果-

致命错误:允许的内存大小 1073741824 字节已耗尽

内存限制= 2048mb

对于结果,我需要带有下一个信息的数组

+-------------+-------------+-------------+-----------+
|  product    | 04-01-2024  |  06-01-2024 |  diff     | 
+-------------+-------------+-------------+-----------+
|  платье     | 186574      | 223001      | 36427     |
|  штаны      | 20564       | 10574       | 99000     |
+-------------+-------------+-------------+-----------+
php sql mysql
1个回答
2
投票

不要执行两个查询,使用单个

JOIN
查询。

SELECT t1.product_name, t1.count AS count1, t2.count AS count2, t1.count - t2.count AS res
FROM product AS t1
JOIN product AS t2 ON t1.product_name = t2.product_name
WHERE t1.date = :first_date AND t2.date = :first_date
© www.soinside.com 2019 - 2024. All rights reserved.