简体   繁体   English

SELECT 语句在不满足条件时应返回 0

[英]SELECT Statement should return 0 when condition is not met

How can I make following MYSQL statment return 0 in case that the condition is not met.如果不满足条件,我如何使以下 MYSQL 语句返回 0。

(SELECT Sum(B.trblksize) AS RxData
 FROM   referencecoexistence_ber_lte B,
        (SELECT C.*
         FROM   referencecoexistence C
         WHERE  `sw_ver` = '0.4'
                AND `lte_n_frames` = '50'
                AND `lte_rb` = '6'
                AND `lte_mcs` = '11'
                AND `lte_p_mw` = '6.000000e-03'
                AND `lte_vmsf_type` = 'BS'
                AND `lte_vmsf_subframes` = '8 9'
                AND `wlan_mcs` = '5'
                AND `wlan_p_mw` = '100'
                AND `channel` = 'A330'
                AND `lte_freq_hz` = '2403000000'
                AND `wlan_freq_hz` = '2412000000'
                AND `wlan_ieee` = '802.11n') AS TableX
 WHERE  TableX.id = B.id
        AND B.ber = '0'
 GROUP  BY lte_dist_m)  

The result is: Empty set (0.280 sec) If the condition includes B.ber = '0' the expected output is: A result with b.ber = 0 looks like:结果是:空集(0.280 秒)如果条件包括 B.ber = '0',则预期的 output 是:b.ber = 0 的结果如下所示:
RxData接收数据
416342016 416342016
433004544 433004544
... ...
In my case it would be sufficient if it simply returns one entry:在我的情况下,如果它只返回一个条目就足够了:
RxData接收数据
0 0

This statement is embedded in a larger statement that calculates some throughput.该语句嵌入在计算某些吞吐量的较大语句中。

Is it possible without executing the statement twice?是否可以不执行该语句两次?

Here we'll change this to a LEFT OUTER JOIN.在这里,我们将其更改为 LEFT OUTER JOIN。 We will move the B.ber = '0' condition into the FROM clause, because with an outer join, this will give us different results than if the condition is in the WHERE clause.我们将 B.ber = '0' 条件移动到 FROM 子句中,因为使用外连接,这将给我们带来与条件在 WHERE 子句中不同的结果。

SELECT Sum(B.trblksize) AS RxData
FROM (SELECT C.*
      FROM   referencecoexistence C
      WHERE  `sw_ver` = '0.4'
        AND `lte_n_frames` = '50'
        AND `lte_rb` = '6'
        AND `lte_mcs` = '11'
        AND `lte_p_mw` = '6.000000e-03'
        AND `lte_vmsf_type` = 'BS'
        AND `lte_vmsf_subframes` = '8 9'
        AND `wlan_mcs` = '5'
        AND `wlan_p_mw` = '100'
        AND `channel` = 'A330'
        AND `lte_freq_hz` = '2403000000'
        AND `wlan_freq_hz` = '2412000000'
        AND `wlan_ieee` = '802.11n') AS TableX
  LEFT OUTER JOIN referencecoexistence_ber_lte B
    ON TableX.id = B.id
       AND B.ber = '0'
 GROUP  BY lte_dist_m

Now when B.ber is zero, it will act like an inner join.现在当 B.ber 为零时,它将像一个内部连接。 However, when B.ber is not zero, then the inner join result will be missing from the join, but the outer join will include the result with all the columns of the B table, but NULL for all the columns of the TableX table.但是,当 B.ber 不为零时,则连接中将缺少内连接结果,但外连接将包含 B 表的所有列的结果,但 TableX 表的所有列的 NULL。 So those records will have B.trblksize as NULL, and their sum will be NULL.所以这些记录的 B.trblksize 为 NULL,它们的总和为 NULL。

I solved the problem by simply using following approach我通过简单地使用以下方法解决了这个问题

(SELECT SUM(IF(T_.BER =0,T_.TrBlkSize,0)) as RxData, Lte_Dist_m FROM 
    (ReferenceCoexistence WHERE <CONDITION LIST>) as X, 
    ReferenceCoexistence_BER_Lte as T_ 
WHERE X.ID = T_.ID GROUP BY Lte_Dist_m)

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

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