Table 1: book(bookid(pk),bookname,edition)
Table 2: bookinfoauthors(bookid(fk), authorid(fk))
Table 3: author(authorid(pk), authorname)
我有AuthorNames的陣列我想通過這些作者撰寫相應BOOKNAME AuthorNames可能含有1個或多個名稱我應該用加入或使用單獨的查詢
我使用PHP和MySQL數據庫
風格1:
select book.bookName
from book, bookauthors, author
where (book.bookid = bookoauthors.bookid)
and (author.authorid = bookauthor.authorid)
and (author.authorName = Authornames[0])
or (author.authorName = Authornames[1])
or (author.authorName = Authornames[2])
or (author.authorName = Authornames[3])
,因爲我使用PHP我的MySQL; 樣式2: //假設$的AuthorID,$ BOOKID,$ BOOKNAME包含int或字符串數組不是對象
$authorId =
select authorId
from authors
where authorName in ($authorNames);
$bookId = select bookid from bookAuthors where bookName in($authorId);
$bookName = select bookName from book where bookid in (bookId);
在第二種風格,我不使用連接哪一個將是有效的,我應該遵循
使用連接..它很快然後正常查詢 –