即使表mysql中找不到任何值,也返回行

时间:2018-03-16 09:03:47

标签: mysql

我有一个选择,我试图返回一行,即使从选择中找不到任何内容。

这是选择

                    select  
                    1 as risk_management,
                    0 as Borrow,  
                    0 as Interest, 
                    IFNULL(d.symbol,'E') as symbol, 
                    IFNULL(d.Abbreviation,'EUR') as Abbreviation, 
                    IFNULL(sum(round((a.amount_financed - a.amount_invested - a.amount_withdrawn) * i.average_rate / j.average_rate, 2)),0)  as LendingOffers,
                    IFNULL( min(a.Interest),0) as InterestLend, 
                    0 as VolumePerDay,
                    0 as LatestId, 
                    0 as InterestLatestRealized,
                    0 as InterestBorrowLow,
                    IFNULL(max(a.Interest),0) as InterestLendHigh
            from    market_cap a
            where   ........more statements here...

但是当我运行这个选择时,我仍然没有得到任何回报。 我希望mysql生成一个数字为0的行,如果缺少值,则为'E'和'EUR',我认为IFNULL在搜索其他stackoverflow之后仍适用于此,但它在我的情况下不起作用。< / p>

1 个答案:

答案 0 :(得分:0)

由于我没有您的数据,我无法为您测试查询,但我可以向您展示基本想法。

您需要创建一个缓冲表,并将默认数据作为查询的主要子选择。在我的示例中,它被称为“ dv ”,如“默认值”。

获取实际值的查询也是from子句中的子查询。在我的示例中,它被称为“ rv ”,如“ real values ”。

我使用left (outer) join来连接两个select语句,条件始终为true(on 1 = 1)。

因此,当获取实际值的查询找不到任何结果时,我们仍然可以使用默认表中的值。

select  
                IFNULL(rv.risk_management, dv.risk_management) as risk_management,
                IFNULL(rv.Borrow, dv.Borrow) as Borrow,
                IFNULL(rv.Interest, dv.Interest) as Interest,
                IFNULL(rv.symbol, dv.symbol) as symbol,
                IFNULL(rv.Abbreviation, dv.Abbreviation) as Abbreviation,
                IFNULL(rv.LendingOffers, dv.LendingOffers) as LendingOffers,
                IFNULL(rv.InterestLend, dv.InterestLend) as InterestLend,
                IFNULL(rv.VolumePerDay, dv.VolumePerDay) as VolumePerDay,
                IFNULL(rv.LatestId, dv.LatestId) as LatestId,
                IFNULL(rv.InterestLatestRealized, dv.InterestLatestRealized) as InterestLatestRealized,
                IFNULL(rv.InterestBorrowLow, dv.InterestBorrowLow) as InterestBorrowLow,
                IFNULL(rv.InterestLendHigh, dv.InterestLendHigh) as InterestLendHigh
            from (
                1 as risk_management,
                0 as Borrow,  
                0 as Interest, 
                'E' as symbol, 
                'EUR' as Abbreviation
                0 as LendingOffers,
                0 as InterestLend,
                0 as VolumePerDay,
                0 as LatestId, 
                0 as InterestLatestRealized,
                0 as InterestBorrowLow,
                0 as InterestLendHigh
            ) as dv
            LEFT JOIN (
                select  
                    risk_management,
                    Borrow,  
                    Interest, 
                    d.symbol, 
                    d.Abbreviation, 
                    sum(round((a.amount_financed - a.amount_invested - a.amount_withdrawn) * i.average_rate / j.average_rate, 2)) as LendingOffers,
                    min(a.Interest) as InterestLend, 
                    VolumePerDay,
                    LatestId, 
                    InterestLatestRealized,
                    InterestBorrowLow,
                    max(a.Interest) as InterestLendHigh
                from market_cap a
                where   ........more statements here...
            ) AS rv
            ON 1 = 1

很好的成功,并有一个美好的一天Masnad Nihit