如何将这两个查询与多个连接组合在一起?

时间:2018-01-16 14:30:06

标签: mysql join

所以我需要在word_pdf_cords.lineNumber上加入两个查询。这两个查询也包含连接。不知道MySQL进出的语法,所以我有点挣扎。

查询一个

Select pdf.html_path,pdf.xml_path, 
word.word_Value,word_pdf.word_pdf_ID,word_pdf_cords.word_pdf_cords_ID,
word_pdf_cords.word_pdf_ID,word_pdf_cords.pageNumber,
word_pdf_cords.lineNumber 

FROM word_pdf

INNER JOIN pdf ON pdf.PDF_ID = word_pdf.PDF_ID

Inner Join word ON word.word_ID = word_pdf.word_ID

Inner Join word_pdf_cords ON word_pdf.word_pdf_ID = 
word_pdf_cords.word_pdf_ID

WHERE word.word_Value ="clocks"

查询两个

Select pdf.html_path,pdf.xml_path, 
word.word_Value,word_pdf.word_pdf_ID,word_pdf_cords.word_pdf_cords_ID,
word_pdf_cords.word_pdf_ID,word_pdf_cords.pageNumber,
word_pdf_cords.lineNumber 

FROM word_pdf

INNER JOIN pdf ON pdf.PDF_ID = word_pdf.PDF_ID

Inner Join word ON word.word_ID = word_pdf.word_ID

Inner Join word_pdf_cords ON word_pdf.word_pdf_ID = 
word_pdf_cords.word_pdf_ID

WHERE word.word_Value ="pirates"

我需要在pdf_cords.lineNumber上加入这两个查询的结果。

2 个答案:

答案 0 :(得分:0)

据我所知,唯一的区别在于WHERE子句,所以不是加入两个查询,你只能加入where子句:

WHERE word.word_Value ="pirates" OR word.word_Value ="clocks"

答案 1 :(得分:0)

这似乎有效,但不适用于大量数据。如果有人更了解数据库查询优化,可以给我一些很好的指导。

Select 

q1.html_path,q1.xml_path,q1.word_Value,q1.pageNumber,q1.lineNumber,
q2.word_Value FROM

 (SELECT pdf.html_path,pdf.xml_path, 
word.word_Value,word_pdf_cords.pageNumber,
word_pdf_cords.lineNumber FROM word_pdf INNER JOIN pdf ON pdf.PDF_ID = 
word_pdf.PDF_ID Inner Join word ON word.word_ID = word_pdf.word_ID Inner 
Join word_pdf_cords ON word_pdf.word_pdf_ID = word_pdf_cords.word_pdf_ID 
WHERE word.word_Value ="John")

as q1

 JOIN (SELECT pdf.html_path,pdf.xml_path, 
word.word_Value,word_pdf_cords.pageNumber,word_pdf_cords.lineNumber FROM 
word_pdf INNER JOIN pdf ON pdf.PDF_ID = word_pdf.PDF_ID Inner Join word ON 
word.word_ID = word_pdf.word_ID Inner Join word_pdf_cords ON 
word_pdf.word_pdf_ID = word_pdf_cords.word_pdf_ID WHERE word.word_Value 
="Smith")

as q2 ON q1.html_path = q2.html_path and q1.pageNumber = 

q2.pageNumber and q1.lineNumber = q2.lineNumber