我有以下3个SQL表:
mysql> select * FROM borrower;
+--------+------------+---------+--------------+
| cardno | name | address | phone |
+--------+------------+---------+--------------+
| 1 | A | nj,usa | 111-222-333 |
| 2 | B | NY,USA | 444-555-666 |
| 3 | C | nj,usa | 777-888-999 |
+--------+------------+---------+--------------+
3 rows in set (0.00 sec)
mysql> select * FROM bookloans;
+--------+----------+--------+------------+------------+
| bookid | branchid | cardno | dateout | duedate |
+--------+----------+--------+------------+------------+
| 1 | 1 | 1 | 2014-04-01 | 2014-08-01 |
| 2 | 1 | 1 | 2014-04-02 | 2014-08-02 |
| 3 | 2 | 2 | 2014-04-04 | 2014-07-05 |
+--------+----------+--------+------------+------------+
1 row in set (0.00 sec)
mysql> select * FROM books;
+--------+------------+---------+------
| bookid | title | publishername |
+--------+------------+---------+------
| 1 | Atitle | tmh |
| 2 | Btitle | tmh |
| 3 | Ctitle | tmh |
+--------+------------+---------+-----+
3 rows in set (0.00 sec)
mysql> select * FROM librarybranch;
+----------+------------+--------+
| branchid | branchname | address|
+--------+----------+--------+---+
| 1 | abc loc | nj,usa |
| 2 | def loc | NY,USA |
+----------+------------+---------+
2 rows in set (0.00 sec)
现在我想执行一个查询,显示书名,借用人姓名,借款人地址 每本书由“def loc”分支借出,其分支是今天(2014-07-05)。我上来了 有两个可能的查询:
1.基于联接的查询:
select books.title,borrower.name,librarybranch.branchname from (( bookloans inner join
borrower on bookloans.cardno = borrower.cardno) inner join books on bookloans.bookid =
books.bookid inner join librarybranch on bookloans.branchid=librarybranch.branchid)
where duedate='2014-07-05' and branchname="def loc";
2.基于多个ands的查询:
select b.title,r.name,r.address from books b,borrower r,bookloans bl,librarybranch lb
where lb.branchname='def loc' and lb.branchid = bl.branchid and bl.duedate='2014-07-05'
and bl.cardno=r.cardno and bl.bookid=b.bookid;
对我来说,基于联接的查询是最直观的,我们正在创建一个巨型表,其中来自不同表的所有行连接在一起,然后基于来自多个表的列查询这个巨型表。但是我发现基于多个ands的查询给出相同的结果令人惊讶。我的问题是上面的选项2(一个基于多个ands)完全等同于选项1(执行内部联接)。如果没有,有什么区别?在这种情况下,它们似乎给出了相同的结果。