2015-09-19 86 views
0

我的目標是在表格中顯示來自DOC_sql和MDT_sql的數據。找不到應該如何實施這種聯繫的解決方案。Mysql在處理後連接兩個連續表格

目標是搜索文檔並根據匹配的標籤呈現文檔。

輸入數據

CALL SearthDocuments('\'Book\',\'Currency\'', 'en_ENG'); 

代碼

DELIMITER // 
CREATE PROCEDURE SearthDocuments(IN VAR_TT_names VARCHAR(255), VAR_LANG_prefix CHAR(6)) 
BEGIN 

SET @TT_sql = CONCAT('SELECT TAG_ID FROM TagTranslate WHERE TT_text IN (', VAR_TT_names, ')'); 
SET @DOC_ID_sql = CONCAT('SELECT DOC_ID FROM DocumentTag WHERE TAG_ID IN (', @TT_sql, ') GROUP BY DOC_ID HAVING COUNT(DOC_ID)'); 

SET @DOC_sql = CONCAT('SELECT * FROM VIEW_Document WHERE DOC_ID IN (', @DOC_ID_sql, ') AND LANG_prefix = "', VAR_LANG_prefix, '"'); 
SET @MDT_sql = CONCAT('SELECT DOC_ID, count(*) as TAG_HITS FROM DocumentTag WHERE TAG_ID IN (', @TT_sql, ') GROUP BY DOC_ID HAVING COUNT(DOC_ID)'); 

-- SET @COM_sql = CONCAT('?') 
-- PREPARE COM_stmt FROM @COM_sql; 
-- EXECUTE COM_stmt; 
-- DEALLOCATE PREPARE COM_stmt; 
END 
// DELIMITER ; 

兩個表都有標識符DOC_ID

TABLE:DOC_sql

DOC_ID, DT_ID, DT_desc, DT_title, A_name, LANG_prefix 

TABLE:MDT_sql

DOC_ID, TAG_HITS 
+0

豈不是很好,如果你能說什麼表看起來像 – e4c5

+0

這是一個創建了'concat' SQL語句,並試圖在兩個表執行多個級聯的sql語句一個weired功能。什麼是表結構,數據和應該輸出什麼?底線:要實現的基本任務是什麼? –

回答

0

這是問題的解決方案。工作代碼。

DELIMITER // 
    CREATE PROCEDURE SearthDocuments(IN VAR_TT_names VARCHAR(255), VAR_LANG_prefix CHAR(3)) 
    BEGIN 

    SET @TT_sql = CONCAT('SELECT TAG_ID FROM TagTranslate WHERE TT_text IN (', VAR_TT_names, ')'); 
    SET @DOC_ID_sql = CONCAT('SELECT DOC_ID FROM DocumentTag WHERE TAG_ID IN (', @TT_sql, ') GROUP BY DOC_ID HAVING COUNT(DOC_ID)'); 

    SET @DOC_sql = CONCAT('SELECT * FROM VIEW_Document WHERE DOC_ID IN (', @DOC_ID_sql, ') AND LANG_prefix = ',VAR_LANG_prefix); 
    SET @MDT_sql = CONCAT('SELECT DOC_ID, count(*) as TAG_HITS FROM DocumentTag WHERE TAG_ID IN (', @TT_sql, ') GROUP BY DOC_ID HAVING COUNT(DOC_ID)'); 

    SET @COM_sql = CONCAT(' 
     SELECT * FROM 
     (SELECT * FROM VIEW_Document WHERE DOC_ID IN (', @DOC_ID_sql, ') AND LANG_prefix = "',VAR_LANG_prefix,'") AS V_DOC 
     LEFT JOIN 
     (SELECT DOC_ID, count(*) as TAG_HITS FROM DocumentTag WHERE TAG_ID IN (', @TT_sql ,') GROUP BY DOC_ID HAVING COUNT(DOC_ID)) as Searth 
     on V_DOC.DOC_ID = Searth.DOC_ID ORDER BY TAG_HITS DESC;' 
    ); 

    PREPARE COM_stmt FROM @COM_sql; 
    EXECUTE COM_stmt; 
    DEALLOCATE PREPARE COM_stmt; 

END 
// DELIMITER ;