如何组合两个具有不同 WHERE / LIKE 条件的查询?

How can I combine two queries with different WHERE / LIKE conditions?

我的两个查询:

$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

为什么不在 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?
");

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

$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%'
");

您可以将 where's 组合成一个 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
");