Php 我应该使用Join还是使用separate查询

Php 我应该使用Join还是使用separate查询,php,mysql,join,Php,Mysql,Join,我有一个AuthorNames数组,我想得到这些作者编写的相应书名 AuthorNames可能包含一个或多个名称 我正在使用php和mysql数据库 风格1: Table 1: book(bookid(pk),bookname,edition) Table 2: bookinfoauthors(bookid(fk), authorid(fk)) Table 3: author(authorid(pk), authorname) 当我使用php时,我的mysql; 风格2: //假设$autho

我有一个AuthorNames数组,我想得到这些作者编写的相应书名 AuthorNames可能包含一个或多个名称

我正在使用php和mysql数据库

风格1:

Table 1: book(bookid(pk),bookname,edition)
Table 2: bookinfoauthors(bookid(fk), authorid(fk))
Table 3: author(authorid(pk), authorname)
当我使用php时,我的mysql; 风格2: //假设$authorId、$bookId、$bookName包含int或string非对象数组

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])

在第二种风格中,我没有使用连接,哪种连接更有效,我应该遵循什么?首先,我想说,您几乎肯定宁愿执行单个
JOIN
查询来获得单个结果集,而不是对MySQL数据库进行许多不同的调用。MySQL是为繁重的数据而设计的;更不用说了。在后一种情况下,花费在网络流量上的时间可能会很长,从而影响站点的性能

其次,您应该尝试使用符合ANSI-92的SQL查询。因此,我将您的
JOIN
查询改写为:

$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);

首选这种查询方式的原因是,它将表的联接与
WHERE
子句中的其他限制分离开来。在原始查询中,联接条件和限制同时出现在
WHERE
子句中,使其更难阅读。

使用联接。。这比普通的问题快谢谢我真的认为第二个会更好
SELECT b.bookName
FROM book b INNER JOIN bookauthors ba ON b.bookid = ba.bookid
            INNER JOIN author a ON a.authorid = ba.authorid
WHERE a.authorName = Authornames[0] OR
      a.authorName = Authornames[1] OR
      a.authorName = Authornames[2] OR
      a.authorName = Authornames[3]