2017-09-21 81 views
0

我執行的查詢結果中沒有空值。下面是我query使用MySQL排除數據庫結果表中的空值

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

任何幫助將高度讚賞還是我得到空值

+0

見https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very -simple-sql-query – Strawberry

回答

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 
+0

我試過了,但是出現錯誤'錯誤代碼:1064 你的SQL語法錯誤;檢查對應於您MariaDB的服務器版本使用附近的正確語法手冊「.Zong ,MAX(sn.Mobilink)AS sn.Mobilink 根據調查河畔 INNER JOIN( SELE」在行26' – faisal1208

+0

@ faisal1208 - 對不起,試試我編輯的答案 –

+0

沒有'sn.zong'或者'sn.mobilink'這樣的東西 – faisal1208