简体   繁体   中英

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.

(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:
RxData
416342016
433004544
...
In my case it would be sufficient if it simply returns one entry:
RxData
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. 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.

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. 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. So those records will have B.trblksize as NULL, and their sum will be 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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