如何将两个查询与不同的WHERE / LIKE条件组合在一起?

时间:2015-12-14 19:30:48

标签: php sql mysqli

我的两个疑问:

$compareTotals1 = mysqli_query($con,"
    SELECT *,   (SUM(rent_amount)+SUM(late_fees)-SUM(discount_amount)) AS total,
                SUM(late_fees) AS latefees,
                SUM(discount_amount) AS discounts
    FROM        transaction
    WHERE       paid_on LIKE '%2014%'
");
$compareTotals2 = mysqli_query($con,"
    SELECT *,   (SUM(rent_amount)+SUM(late_fees)-SUM(discount_amount)) AS total,
                SUM(late_fees) AS latefees,
                SUM(discount_amount) AS discounts
    FROM        transaction
    WHERE       paid_on LIKE '%2015%'
");

我如何输出结果:

if ($row = mysqli_fetch_array($compareTotals1)) {
    echo CURRENCY.number_format($row['total'],2);
    echo CURRENCY.number_format($row['latefees'],2);
    echo CURRENCY.number_format($row['discounts'],2);
} else { 
    echo "No Records."; 
}

if ($row = mysqli_fetch_array($compareTotals2)) {
    echo CURRENCY.number_format($row['total'],2);
    echo CURRENCY.number_format($row['latefees'],2);
    echo CURRENCY.number_format($row['discounts'],2);
} else { 
    echo "No Records."; 
}

paid_on LIKE '% %'是由下拉框和一些javascript动态生成的。这是唯一改变的部分。

如何将其压缩为一个查询,因此我只需要使用一个mysqli_fetch_array

2 个答案:

答案 0 :(得分:1)

假设您想要多行,那么联合可能是最干净的

$compareTotals1 = mysqli_query($con,"
    SELECT *,   (SUM(rent_amount)+SUM(late_fees)-SUM(discount_amount)) AS total,
                SUM(late_fees) AS latefees,
                SUM(discount_amount) AS discounts,
                '2014' as Yr
    FROM        transaction
    WHERE       paid_on LIKE '%2014%'
    UNION ALL
    SELECT *,   (SUM(rent_amount)+SUM(late_fees)-SUM(discount_amount)) AS total,
                SUM(late_fees) AS latefees,
                SUM(discount_amount) AS discounts,
                '2015' as Yr
    FROM        transaction
    WHERE       paid_on LIKE '%2015%'
");

您可以将地点组合成OR并确保拼出一个群组或者您不知道哪个是2015年或2014年,除非*包含此类详细信息。

$compareTotals1 = mysqli_query($con,"
    SELECT *,   (SUM(rent_amount)+SUM(late_fees)-SUM(discount_amount)) AS total,
                SUM(late_fees) AS latefees,
                SUM(discount_amount) AS discounts,
                case when paid_on like '%2014%' then '2014' 
                     when paid_on like '%2015%' then '2015' end as yr
    FROM        transaction
    WHERE       paid_on LIKE '%2014%'
       OR       paid_on LIKE '%2015%'
   --GROUP BY    all fields from select relevant to group by... without structure and sample data from table can't figure out.
   -- This might work though I'd be concerned all the * columns could be returning improper results.
   GROUP BY case when paid_on  like '%2014%' then '2014' when paid_on like '%2015%' then '2015' end
");

也许...... group by case when paid_on like '%2014%' then '2014' when paid_on like '%2015%' then '2015' end但这非常具体。

我们或许可以通过paid_on进行分组,但看起来它不仅仅是一年......所以你每年可能会得到多行......所以如果没有结构的样本数据,也无法弄清楚要做什么。

或许您想要交叉加入更多列...而不是更多行......

$compareTotals1 = mysqli_query($con,"
    Select * from (
    SELECT *,   (SUM(rent_amount)+SUM(late_fees)-SUM(discount_amount)) AS total,
                SUM(late_fees) AS latefees,
                SUM(discount_amount) AS discounts
    FROM        transaction
    WHERE       paid_on LIKE '%2014%') CROSS JOIN

    (SELECT *,   (SUM(rent_amount)+SUM(late_fees)-SUM(discount_amount)) AS total,
                SUM(late_fees) AS latefees,
                SUM(discount_amount) AS discounts
    FROM        transaction
    WHERE       paid_on LIKE '%2015%') B
");

答案 1 :(得分:0)

为什么不在WHERE中使用OR?

$compareTotals1 = mysqli_query($con,"
    SELECT *,   (SUM(rent_amount)+SUM(late_fees)-SUM(discount_amount)) AS total,
                SUM(late_fees) AS latefees,
                SUM(discount_amount) AS discounts
    FROM        transaction
    WHERE       paid_on LIKE '%2014%'
        OR      paid_on LIKE '%2015%'
    GROUP BY    YEAR(paid_on) -- is `paid_on` a date?
");