这两个SQL JOINS之间有区别吗?

时间:2019-05-02 09:26:05

标签: sql

假设我们有这三个表:

水手(编号,名称,评分)保留(编号,出价,日期)船(出价,名称,颜色)

我想将Table Sailors与Table Reserve一起加入。以下语句是否等效?有人更有效吗?

SELECT S.sid, S.name, R.bid  
FROM Sailors as S,
     Reserves as R   
WHERE S.sid = R.sid 

SELECT S.sid, S.name, R.bid   
FROM Sailors as S
INNER JOIN Reserves  as R  ON S.sid = R.sid

谢谢!

1 个答案:

答案 0 :(得分:1)

完全一样,尽管更新的显式联接样式被认为是更好的实现方式。它更易于阅读,调试和维护。

DECLARE @Sailors TABLE (sid INT, name VARCHAR(10), rating INT)
DECLARE @Reserve TABLE (sid INT, bid INT, day VARCHAR(10))

INSERT INTO @Sailors VALUES
(1,'name', 10),
(2,'name1', 5),
(3,'name2', 2),
(4,'name3', 3)

INSERT INTO @Reserve VALUES
(1,1,'Monday'),
(2,2,'Monday'),
(3,3,'Monday')

声明:

SELECT
s.sid,s.name,r.bid
FROM @Sailors AS s, @Reserve AS r
WHERE s.sid = r.sid

返回:

sid name    bid
1   name    1
2   name1   2
3   name2   3

SELECT
s.sid,s.name,r.bid
FROM @Reserve r
INNER JOIN @Sailors s ON r.sid = s.sid

返回

sid name    bid
1   name    1
2   name1   2
3   name2   3