简体   繁体   English

如何将两个查询与不同的WHERE / LIKE条件结合使用?

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

My two queries: 我的两个查询:

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

How I'm outputting results: 我如何输出结果:

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."; 
}

The paid_on LIKE '% %' is generated dynamically by a dropdown box and some javascript. 由下拉框和一些javascript动态生成paid_on LIKE '% %' That's the only part that changes. 那是唯一改变的部分。

How can I condense this into one query so I only need to use one mysqli_fetch_array ? 如何将其压缩为一个查询,所以我只需要使用一个mysqli_fetch_array

Assuming you want multiple rows a union is likely the cleanest 假设您想要多个行,则工会可能是最干净的

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

You may be able to combine the where's into an OR and be sure to spell out a group by or you will not know which is 2015 or 2014 unless * includes such details. 您可能可以将“哪里”合并为一个“ 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
");

maybe... group by case when paid_on like '%2014%' then '2014' when paid_on like '%2015%' then '2015' end but this is very specific. 也许... group by case when paid_on like '%2014%' then '2014' when paid_on like '%2015%' then '2015' end但这是非常具体的。

we might be able to group by paid_on, but it appears it's not just a year... so you may get multiple rows per year... so again without sample data for structure can't figure out what to do. 我们也许可以按paid_on进行分组,但看来这不只是一年……所以您每年可能会得到多行……因此同样,如果没有用于结构的样本数据也无法弄清楚该怎么做。

Or maybe you want a cross join more columns... not more rows... 或者,也许您想交叉连接更多列...而不是更多行...

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

Why not just use an OR in your WHERE? 为什么不只在您的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?
");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM