douyou8266 2014-12-01 09:01
浏览 49
已采纳

mysql将三个查询合并为一个

I am calculating purchase, return, sales seperately from a single table for this i have query as follows.

$stmt = $pdo->prepare("SELECT sum(quantity) as quantity_in, sum(total) as total_in FROM silk WHERE full_name = '$full_name' AND (type='purchase') AND sale_date BETWEEN '$from_date' and '$to_date'"); $stmt->execute();
$products_in = $stmt->fetchAll();
foreach($products_in as $product_in){
                        $purchase = $product_in['quantity_in'];
                    }

                    $stmt = $pdo->prepare("SELECT sum(quantity) as quantity_in, sum(total) as total_in FROM silk WHERE full_name = '$full_name' AND (type='return purchase') AND sale_date BETWEEN '$from_date' and '$to_date'"); $stmt->execute();
                    $products_in = $stmt->fetchAll();
                    foreach($products_in as $product_in){
                        $return_purchase = $product_in['quantity_in'];
                    }

                    $stmt = $pdo->prepare("SELECT sum(quantity) as quantity_in, sum(total) as total_in FROM silk WHERE full_name = '$full_name' AND (type='initial_stock') AND sale_date BETWEEN '$from_date' and '$to_date'"); $stmt->execute();
                    $products_in = $stmt->fetchAll();
                    foreach($products_in as $product_in){
                        $initial_stock = $product_in['quantity_in'];
                    }

The above takes a very long time, if there were 2000 products it takes neraly 5 minutes to calculate, is there any way to combine the above three queries into one, so that it can run FASTER. maybe like this,

(SELECT sum(quantity) as quantity_in, sum(total) as total_in FROM silk WHERE full_name = '$full_name' AND (type='initial_stock'))
as opening, 
(SELECT sum(quantity) as quantity_in, sum(total) as total_in FROM silk WHERE full_name = '$full_name' AND (type='purchase')) as purchase
WHERE
sale_date BETWEEN '$from_date' and '$to_date'

Note: im not good at MySQL, I've only run simple queries through PHP until now.

  • 写回答

3条回答 默认 最新

  • duanjihe5180 2014-12-01 09:09
    关注

    I would use an group by statement where you can query everything in one query.

    SELECT type, sum(quantity) as quantity_in, sum(total) as total_in 
    FROM silk 
    WHERE full_name = '$full_name' AND sale_date BETWEEN '$from_date' and '$to_date'
    group by type
    

    And in that case it would give you this in your PHP Code

    $stmt = $pdo->prepare("SELECT type, sum(quantity) as quantity_in, sum(total) as total_in FROM silk WHERE full_name = '$full_name' AND sale_date BETWEEN '$from_date' and '$to_date' group by type"); $stmt->execute();
    $products_in = $stmt->fetchAll();
    foreach($products_in as $product_in){
        switch ($product_in['type']) {
            case 'purchase':
                $purchase = $product_in['quantity_in'];
            break;
            case 'return purchase':
                $return_purchase = $product_in['quantity_in']
            break;
            case 'initial_stock':
                $initial_stock = $product_in['quantity_in'];
            break;
            // and so on...
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao