mysql将三个查询合并为一个

时间:2014-12-01 09:01:26

标签: php mysql

我正在从单个表中单独计算购买,退货,销售,我的查询如下。

$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'];
                    }

以上需要很长时间,如果有2000个产品需要花费5分钟来计算,有没有办法将上述三个查询合并为一个,这样它就可以运行得更快。 也许是这样,

(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'

注意:我不擅长MySQL,直到现在我才通过PHP运行简单的查询。

3 个答案:

答案 0 :(得分:1)

为什么不使用GROUP BY条款?

SELECT type, SUM(quantity) AS quantity_in, SUM(total) AS total_in
FROM silk
WHERE full_name = '$full_name'
AND type IN ('purchase', 'return purchase', 'initial_stock')
AND sale_date BETWEEN '$from_date' and '$to_date'
GROUP BY type

答案 1 :(得分:0)

尝试

$stmt = $pdo->prepare("SELECT type, 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' GROUP BY type"); 
$stmt->execute();
$products_in = $stmt->fetchAll();

答案 2 :(得分:0)

我会使用group by语句,您可以在一个查询中查询所有内容。

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

在这种情况下,它会在你的PHP代码中给你这个

$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...
    }
}