我正在构建一个搜索页面,其中表单将变量传递给SQL查询。如果我输入任何搜索参数,我得到我想要的结果,除非我将作者或主题搜索留空,如果有多个作者和/或主题与结果相关,我将得到多个结果。我的大多数记录都有多位作者或多个科目。
基本问题是如果有一个记录有2位作者和3个相关科目,那么我得到6个结果。
我想要实现的目标:我正在尝试修改查询,以便我只收到一条记录(record_ID是唯一的),即使有多个作者或多个科目。
我遇到问题的查询中的2个JOINS是拉动作者和主题。例如,为了获得作者,我使用这个连接(主题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,author],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)
我相信你只想按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
这将获取la。*的 last 条目。如果您想要以逗号分隔的作者列表,可以使用group_concat()。
答案 1 :(得分: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