2013-04-15 247 views
0

我正在構建一個搜索頁面,表單將變量傳遞給SQL查詢。如果我輸入任何搜索參數,我會得到我想要的結果,除非我將作者或主題搜索留空,如果有多個作者和/或與結果相關的主題,我會得到多個結果。我的大部分記錄都有多個作者或多個主題。顯示多個結果的MySQL查詢

基本問題是,如果有一個記錄有2個作者和3個相關主題,那麼我會得到6個結果。

我試圖達到的目標:我試圖修改查詢,以便只接收一條記錄(record_ID是唯一的),即使有多個作者或多個主題。

查詢中的2個連接有問題是拉作者和主題。例如,對於得到的作者,我用這個連接(主題JOIN幾乎是相同的)和變量$ fauthor從PHP傳入:

SELECT la.author, la.author_ID, ra.record_ID 
FROM lib_author AS la 
JOIN record_author AS ra ON la.author_ID = ra.author_ID 
JOIN lib_record AS lr ON ra.record_ID = lr.record_ID 
WHERE la.author LIKE '%$fauthor%' 

相關的表是lib_author [AUTHOR_ID,作者] lib_record [RECORD_ID,...],並record_author [RECORD_ID,AUTHOR_ID]

下面是完整的查詢:

select a.record_ID, a.year, n.title, y.author, y.author_ID, j.journal, pox.publisher, lox.language, a.volume, a.issue, a.pages, a.abstract, s.subject 
    FROM lib_record AS a 
    JOIN lib_title as n on n.title_ID = a.title_ID 
    JOIN (SELECT la.author, la.author_ID, ra.record_ID 
     FROM lib_author AS la 
     JOIN record_author AS ra ON la.author_ID = ra.author_ID 
     JOIN lib_record AS lr ON ra.record_ID = lr.record_ID 
      WHERE la.author LIKE '%$fauthor%') AS y 
    JOIN (SELECT ls.subject, ls.subject_ID, rs.record_ID 
     FROM lib_subject AS ls 
     JOIN record_subject AS rs ON ls.subject_ID = rs.subject_ID 
     JOIN lib_record AS lr ON rs.record_ID = lr.record_ID 
      WHERE ls.subject LIKE '%$fsubject%') AS s 
    JOIN lib_journal AS j on j.journal_ID = a.journal_ID 
    JOIN lib_publisher pox ON a.publisher_ID = pox.publisher_ID 
    JOIN lib_language lox ON a.language_ID = lox.language_ID 
    WHERE y.record_ID = a.record_ID 
    AND s.record_ID = a.record_ID 
    AND n.title LIKE '%$ftitle%' 
    AND j.journal LIKE '%$fjournal%' 

我希望有人有一些見解。謝謝

+0

如果您有多個作者,列出,預期的結果是什麼?只需選擇第一個,或者抓住逗號分隔的列表? –

+0

預期結果是顯示與記錄相關的數據的單行。該行是一個href,將用戶發送到卡片目錄並顯示所有信息。 –

回答

0

我相信你只是想按record_ID分組結果。使用您的簡單查詢:

 
SELECT la.author, la.author_ID, ra.record_ID 
    FROM lib_author AS la 
    JOIN record_author AS ra ON la.author_ID = ra.author_ID 
    JOIN lib_record AS lr ON ra.record_ID = lr.record_ID 
    WHERE la.author LIKE '%$fauthor%' 
    GROUP BY ra.record_ID 

這將搶拉的最後進入*。如果你想用逗號分隔的作者列表,你可以使用group_concat()。

+0

Hi @Doug Kress,感謝您的回覆。按* .record_ID分組不起作用。我在報告中給出的例子說明,如果一個記錄有2個作者和3個主題,我將得到2 x 3個結果或6個行,每個記錄具有相同的記錄標識 –

+0

如果你有GROUP BY記錄標識, 6記錄到單個記錄中(有點含糊地選擇作者/ author_ID,除非你使用'ORDER_BY'或其他東西)。我錯過了什麼嗎? –

+0

其實沒有。 GROUP BY的作品!只是不在你想的地方。在查詢的末尾輸入「AND j.journal LIKE'%$ fjournal%'」後,我輸入了下面的「GROUP BY a.record_ID」,並得到我正在尋找的內容。很簡單,很難看到。 –

0

以下是在末尾添加GROUP BY的最終查詢,並將GROUP_CONCAT添加到作者和主題JOINS以顯示完整的數據集。感謝@Doug Kress的幫助!我只是一個志願者網站管理員:

select a.record_ID, a.year, n.title, GROUP_CONCAT(DISTINCT y.author SEPARATOR '; ') AS author, y.author_ID, j.journal, pox.publisher, lox.language, a.volume, a.issue, a.pages, a.abstract, GROUP_CONCAT(DISTINCT s.subject SEPARATOR '; ') AS subject 
    FROM lib_record AS a 
    JOIN lib_title as n on n.title_ID = a.title_ID 
    JOIN (SELECT la.author, la.author_ID, ra.record_ID 
     FROM lib_author AS la 
     JOIN record_author AS ra ON la.author_ID = ra.author_ID 
     JOIN lib_record AS lr ON ra.record_ID = lr.record_ID 
      WHERE la.author LIKE '%$fauthor%') AS y 
    JOIN (SELECT ls.subject, ls.subject_ID, rs.record_ID 
     FROM lib_subject AS ls 
     JOIN record_subject AS rs ON ls.subject_ID = rs.subject_ID 
     JOIN lib_record AS lr ON rs.record_ID = lr.record_ID 
      WHERE ls.subject LIKE '%$fsubject%') AS s 
    JOIN lib_journal AS j on j.journal_ID = a.journal_ID 
    JOIN lib_publisher pox ON a.publisher_ID = pox.publisher_ID 
    JOIN lib_language lox ON a.language_ID = lox.language_ID 
    WHERE y.record_ID = a.record_ID 
    AND s.record_ID = a.record_ID 
    AND n.title LIKE '%$ftitle%' 
    AND j.journal LIKE '%$fjournal%' 
    GROUP BY a.record_ID