更新:該問題似乎是固定的,它發生是由於我在聲明時使用了不同的小寫字母而不是大寫字母。但是,我現在面臨的問題是「vw_formdatahard」視圖中只填充了多個「NULL」。我在這裏瘋了。生成的視圖填充NULL
我有一個程序,它將根據一個Wordpress & Buddypress數據庫中的可用數據創建視圖。這在我的本地Mamp服務器上運行得很好,但每當我在我的託管服務器上調用該過程時都會給我一個錯誤。即使我將整個數據庫從本地服務器複製到在線服務器,它仍會返回錯誤。我不是mysql的英雄,所以我要在這裏深入瞭解......爲什麼不能運行?
以下查詢失敗:「CALL
stp_FormQueryBuilder
(); MySQL表示:#1054 - 未知列在 '字段列表' d.user_id「
BEGIN
DECLARE sqlText text;
DROP VIEW IF EXISTS vw_FormDataRows;
CREATE VIEW vw_FormDataRows AS
SELECT d.user_id, f.name,
CASE WHEN left(f.name, 4) = 'FOTO' && LEFT(d.value, 9) = '/profiles' THEN CONCAT("/wp-content/uploads",d.value)
ELSE d.value END AS 'value'
FROM wp_bp_xprofile_fields AS F
LEFT JOIN wp_bp_xprofile_data AS D ON F.id = D.field_id
WHERE
parent_id = 0
AND group_id = 1
AND d.user_id IN(SELECT U.user_id FROM wp_usermeta AS U WHERE U.meta_key = 'wp_user_level' AND U.meta_value = '0')
ORDER BY d.user_id, f.id;
DROP TEMPORARY TABLE IF EXISTS temp1;
CREATE TEMPORARY TABLE temp1 (SELECT b.user_id, b.name, b.value
FROM (SELECT d.user_id, f.name, d.value
FROM wp_bp_xprofile_fields AS F
LEFT JOIN wp_bp_xprofile_data AS D ON F.id = D.field_id
WHERE
parent_id = 0
AND group_id = 1
AND d.user_id IN(SELECT U.user_id FROM wp_usermeta AS U WHERE U.meta_key = 'wp_user_level' AND U.meta_value = '0')
ORDER BY d.user_id, f.id) AS B);
SET @sql = NULL;
SET group_concat_max_len = 4096;
SELECT GROUP_CONCAT(DISTINCT
CONCAT('MAX(CASE WHEN name = ', name,
' THEN value END) AS `', replace(name, ' ','_'), '`'))
INTO sqlText
FROM temp1;
DROP VIEW IF EXISTS vw_FormDataHard;
SET @sql = CONCAT('CREATE VIEW vw_FormDataHard AS SELECT user_id, ', sqlText, '
FROM vw_FormDataRows
GROUP BY user_id');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
DROP TEMPORARY TABLE IF EXISTS temp1;
END
只需注意,由於您在where子句中擁有正確的旁邊條件條件,所以左邊的連接會像常規的內部連接一樣執行。 – jarlh
更改爲D.user_id – Rijin
也許是因爲表別名是'D',並且您使用'd'來選擇 – Bert