2016-01-18 179 views
1

我有一個SQL查詢,它只是在每個字段中返回一個NULL值。它爲數據庫中輸入的數據量返回正確的行數,但每個列值顯示爲NULL而不是MySQL中字段的值。SQL查詢返回每個值爲NULL

任何人都可以爲我提供任何建議嗎?

SELECT items.form_id as 'Form ID', 
MAX(IF('field_id' LIKE 88,meta_value,NULL)) AS 'User ID', 
MAX(IF('field_id' LIKE 96,meta_value,NULL)) AS 'Display Name', 
MAX(IF('field_id' LIKE 95,meta_value,NULL)) AS 'User Role', 
MAX(IF('field_id' LIKE 301,meta_value,NULL)) AS 'Your District', 
MAX(IF('field_id' LIKE 97,meta_value,NULL)) AS 'Time Duty Logged', 
MAX(IF('field_id' LIKE 84,meta_value,NULL)) AS 'Date of Duty', 
MAX(IF('field_id' LIKE 85,meta_value,NULL)) AS 'Duty Start Time', 
MAX(IF('field_id' LIKE 86,meta_value,NULL)) AS 'Duty End Time', 
MAX(IF('field_id' LIKE 139,meta_value,NULL)) AS 'Duty Status', 
MAX(IF('field_id' LIKE 142,meta_value,NULL)) AS 'Duty Type', 
MAX(IF('field_id' LIKE 174,meta_value,NULL)) AS 'Which Event?', 
MAX(IF('field_id' LIKE 103,meta_value,NULL)) AS 'Total Hours', 
MAX(IF('field_id' LIKE 135,meta_value,NULL)) AS 'Duty Details', 
MAX(IF('field_id' LIKE 136,meta_value,NULL)) AS 'End Section', 
MAX(IF('field_id' LIKE 227,meta_value,NULL)) AS 'User Details', 
MAX(IF('field_id' LIKE 228,meta_value,NULL)) AS 'End Section' 
FROM wp_frm_item_metas as meta 
JOIN wp_frm_items as items ON (items.id = meta.item_id) 
WHERE items.form_id LIKE 6 
GROUP BY meta.item_id 

我已經試過了,現在也跟隨我仍然得到NULL到位每個字段的值

SELECT items.form_id as 'Form ID', 
MAX(IF('field_id' = 88,meta_value,NULL)) AS 'User_ID', 
MAX(IF('field_id' = 96,meta_value,NULL)) AS 'Display_Name', 
MAX(IF('field_id' = 95,meta_value,NULL)) AS 'User_Role', 
MAX(IF('field_id' = 301,meta_value,NULL)) AS 'Your_District', 
MAX(IF('field_id' = 97,meta_value,NULL)) AS 'Time_Duty_Logged', 
MAX(IF('field_id' = 84,meta_value,NULL)) AS 'Date_of_Duty', 
MAX(IF('field_id' = 85,meta_value,NULL)) AS 'Duty_Start_Time', 
MAX(IF('field_id' = 86,meta_value,NULL)) AS 'Duty_End_Time', 
MAX(IF('field_id' = 139,meta_value,NULL)) AS 'Duty_Status', 
MAX(IF('field_id' = 142,meta_value,NULL)) AS 'Duty_Type', 
MAX(IF('field_id' = 174,meta_value,NULL)) AS 'Which_Event?', 
MAX(IF('field_id' = 103,meta_value,NULL)) AS 'Total_Hours', 
MAX(IF('field_id' = 135,meta_value,NULL)) AS 'Duty_Details', 
MAX(IF('field_id' = 136,meta_value,NULL)) AS 'End_Section', 
MAX(IF('field_id' = 227,meta_value,NULL)) AS 'User_Details', 
MAX(IF('field_id' = 228,meta_value,NULL)) AS 'End_Section' 
FROM wp_frm_item_metas as meta 
JOIN wp_frm_items as items ON (items.id = meta.item_id) 
WHERE items.form_id = 6 
GROUP BY meta.item_id 
+0

使用'User_ID'而不是'User ID'等等(沒有空格!) – Tewdyn

+0

field_ID需要反標記而不是撇號以及別名。因爲它將'Field_ID'的比較字符串值設置爲'88',這永遠不會相等。 like運算符也需要%來比較動態值。如果field_ID是數字,我會建議直接向上=''比較 – xQbert

+0

您使用單引號而不是反引號。我投票將這些類型的問題視爲簡單的印刷錯誤。 –

回答

0

兩件事情:您不必引用field_id列名,你可能不要對數字值不想使用LIKE

SELECT items.form_id as 'Form ID', 
MAX(IF(field_id = 88,meta_value,NULL)) AS 'User_ID', 
MAX(IF(field_id = 96,meta_value,NULL)) AS 'Display_Name', 
MAX(IF(field_id = 95,meta_value,NULL)) AS 'User_Role', 
MAX(IF(field_id = 301,meta_value,NULL)) AS 'Your_District', 
MAX(IF(field_id = 97,meta_value,NULL)) AS 'Time_Duty_Logged', 
MAX(IF(field_id = 84,meta_value,NULL)) AS 'Date_of_Duty', 
MAX(IF(field_id = 85,meta_value,NULL)) AS 'Duty_Start_Time', 
MAX(IF(field_id = 86,meta_value,NULL)) AS 'Duty_End_Time', 
MAX(IF(field_id = 139,meta_value,NULL)) AS 'Duty_Status', 
MAX(IF(field_id = 142,meta_value,NULL)) AS 'Duty_Type', 
MAX(IF(field_id = 174,meta_value,NULL)) AS 'Which_Event?', 
MAX(IF(field_id = 103,meta_value,NULL)) AS 'Total_Hours', 
MAX(IF(field_id = 135,meta_value,NULL)) AS 'Duty_Details', 
MAX(IF(field_id = 136,meta_value,NULL)) AS 'End_Section', 
MAX(IF(field_id = 227,meta_value,NULL)) AS 'User_Details', 
MAX(IF(field_id = 228,meta_value,NULL)) AS 'End_Section' 
FROM wp_frm_item_metas as meta 
JOIN wp_frm_items as items ON (items.id = meta.item_id) 
WHERE items.form_id = 6 
GROUP BY meta.item_id 
0
SELECT items.form_id as `Form ID`, 
MAX(IF(`field_id` = 88,meta_value,NULL)) AS `User ID`, 
MAX(IF(`field_id` = 96,meta_value,NULL)) AS `Display Name`, 
MAX(IF(`field_id` = 95,meta_value,NULL)) AS `User Role`, 
MAX(IF(`field_id` = 301,meta_value,NULL)) AS `Your District`, 
MAX(IF(`field_id` = 97,meta_value,NULL)) AS `Time Duty Logged`, 
MAX(IF(`field_id` = 84,meta_value,NULL)) AS `Date of Duty`, 
MAX(IF(`field_id` = 85,meta_value,NULL)) AS `Duty Start Time`, 
MAX(IF(`field_id` = 86,meta_value,NULL)) AS `Duty End Time`, 
MAX(IF(`field_id` = 139,meta_value,NULL)) AS `Duty Status`, 
MAX(IF(`field_id` = 142,meta_value,NULL)) AS `Duty Type`, 
MAX(IF(`field_id` = 174,meta_value,NULL)) AS `Which Event?`, 
MAX(IF(`field_id` = 103,meta_value,NULL)) AS `Total Hours`, 
MAX(IF(`field_id` = 135,meta_value,NULL)) AS `Duty Details`, 
MAX(IF(`field_id` = 136,meta_value,NULL)) AS `End Section`, 
MAX(IF(`field_id` = 227,meta_value,NULL)) AS `User Details`, 
MAX(IF(`field_id` = 228,meta_value,NULL)) AS `End Section` 
FROM wp_frm_item_metas as meta 
JOIN wp_frm_items as items ON (items.id = meta.item_id) 
WHERE items.form_id = 6 
GROUP BY meta.item_id 

多虧了大家的意見 - 這已經完美解決了對我來說。