使用MySQL从数据库结果表中排除空值

时间:2017-09-21 06:50:39

标签: mysql database sqlyog

我正在执行一个查询结果,其中没有空值。以下是我的query

SELECT
  sur.id AS 'Survey ID',
  DATE (sur.capture_date) AS 'Date',
  sur.sub_division AS 'Sub-Div',
  sd.name AS 'Sub-Div Name',
  sd.`circle_name` AS 'Circle Name',
  sur.ref_no AS 'Reference Number',
  sur.customer_id AS 'Customer ID',
  sur.consumer_name AS 'Consumer Name & Address',
  sur.sync_date AS 'Sync Date',
  sur.capture_date AS 'Capture Date',
  sur.latitude AS Latitude,
  sur.longitude AS Longitude,
  sur.near_by_place AS Comments,
  sur.tarrif AS Tariff,
  sur.s_load AS 'Sanctioned Load',
  sur.meter_type AS 'Meter Type (IT)',
  sur.meter_type_field AS 'Meter Type (Field)',
  sur.meter_type_desired AS 'Meter Type (As Desired By SDO)',
  sur.pole_type AS 'Pole Type',
  sur.meter_location AS 'Meter Location',
  sur.est_cable_len AS 'Estimated Cable Length',
  sur.meter_status AS 'Meter Status',
  sur.running_load_entry_a AS 'Running Load Entry (A)',
  sur.running_load_entry_b AS 'Running Load Entry (B)',
  sur.running_load_entry_c AS 'Running Load Entry (C)',
  MAX(CASE WHEN sn.operator_name LIKE '%Zong%' THEN sn.signal_strength END) AS Zong,
  MAX(CASE WHEN sn.operator_name LIKE '%Mobilink%' THEN sn.signal_strength END) AS Mobilink
FROM survey sur
INNER JOIN
  survey_networks sn ON sur.id = sn.survey_id 
INNER JOIN
  survey_hesco_subdivision sd ON sur.sub_division = sd.sub_div_code
WHERE
  sn.`signal_strength` = 'No Signal' AND
  sn.`operator_name` IN ('Zong','Mobilink') AND
  sn.`signal_strength` IS NOT NULL
GROUP BY
  sur.id,
  sur.sub_division,
  sur.ref_no,
  sur.customer_id,
  sur.consumer_name,
  sur.consumer_address ,
  sur.sync_date,
  sur.capture_date,
  sur.source,
  sur.latitude,
  sur.longitude,
  sur.near_by_place,
  sur.tarrif,
  sur.s_load,
  sur.meter_type,
  sur.meter_type_field,
  sur.meter_type_desired,
  sur.pole_type,
  sur.meter_location,
  sur.est_cable_len,
  sur.meter_status,
  sur.running_load_entry_a,
  sur.running_load_entry_b,
  sur.running_load_entry_c 
ORDER BY
  sd.`name` ASC;

如查询所示,我添加了IS NOT NULL,但我仍然获得空值,如下图所示

enter image description here

任何帮助都将受到高度赞赏

1 个答案:

答案 0 :(得分:0)

您正在以这种方式获得结果,因为您要分组的列很多,尝试执行max并在子查询中对这两列进行过滤,如下所示:

SELECT sur.id AS 'Survey ID'
    ,DATE (sur.capture_date) AS 'Date'
    ,sur.sub_division AS 'Sub-Div'
    ,sd.NAME AS 'Sub-Div Name'
    ,sd.`circle_name` AS 'Circle Name'
    ,sur.ref_no AS 'Reference Number'
    ,sur.customer_id AS 'Customer ID'
    ,sur.consumer_name AS 'Consumer Name & Address'
    ,sur.sync_date AS 'Sync Date'
    ,sur.capture_date AS 'Capture Date'
    ,sur.latitude AS Latitude
    ,sur.longitude AS Longitude
    ,sur.near_by_place AS Comments
    ,sur.tarrif AS Tariff
    ,sur.s_load AS 'Sanctioned Load'
    ,sur.meter_type AS 'Meter Type (IT)'
    ,sur.meter_type_field AS 'Meter Type (Field)'
    ,sur.meter_type_desired AS 'Meter Type (As Desired By SDO)'
    ,sur.pole_type AS 'Pole Type'
    ,sur.meter_location AS 'Meter Location'
    ,sur.est_cable_len AS 'Estimated Cable Length'
    ,sur.meter_status AS 'Meter Status'
    ,sur.running_load_entry_a AS 'Running Load Entry (A)'
    ,sur.running_load_entry_b AS 'Running Load Entry (B)'
    ,sur.running_load_entry_c AS 'Running Load Entry (C)'
    ,MAX(sn.Zong) AS sn.Zong
    ,MAX(sn.Mobilink) AS sn.Mobilink
FROM survey sur
INNER JOIN (
    SELECT MAX(CASE 
                WHEN sn.operator_name LIKE '%Zong%'
                    THEN sn.signal_strength
                END) AS Zong
        ,MAX(CASE 
                WHEN sn.operator_name LIKE '%Mobilink%'
                    THEN sn.signal_strength
                END) AS Mobilink
    FROM survey_networks AS sn
    WHERE sn.`signal_strength` = 'No Signal'
        AND sn.`operator_name` IN (
            'Zong'
            ,'Mobilink'
            )
        AND sn.`signal_strength` IS NOT NULL
    ) AS sn ON sur.id = sn.survey_id
INNER JOIN survey_hesco_subdivision sd ON sur.sub_division = sd.sub_div_code
GROUP BY sur.id
    ,sur.sub_division
    ,sur.ref_no
    ,sur.customer_id
    ,sur.consumer_name
    ,sur.consumer_address
    ,sur.sync_date
    ,sur.capture_date
    ,sur.source
    ,sur.latitude
    ,sur.longitude
    ,sur.near_by_place
    ,sur.tarrif
    ,sur.s_load
    ,sur.meter_type
    ,sur.meter_type_field
    ,sur.meter_type_desired
    ,sur.pole_type
    ,sur.meter_location
    ,sur.est_cable_len
    ,sur.meter_status
    ,sur.running_load_entry_a
    ,sur.running_load_entry_b
    ,sur.running_load_entry_c
ORDER BY sd.`name` ASC