Nastro
Nastro

Reputation: 1769

What is the difference between JOIN and simple SELECT in MySQL?

I have two mySQL statements. First is:

SELECT o.OrderID, c.CustomerName, o.OrderDate
FROM Customers AS c, Orders AS o
WHERE c.CustomerID=o.CustomerID;

The second is:

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;

Both produce the same result, but second doesn't contain reference on Customers table in FROM request.

My question is - what is the difference between these two sql statements? In which cases should I use JOIN and in which cases should I use simple SELECT from two tables?

Upvotes: 2

Views: 1054

Answers (2)

D-Shih
D-Shih

Reputation: 46249

Those JOIN are different, although the result are the same.

The First one is CROSS JOIN and adds the condition in where, which is implicit CROSS JOIN

The second one is INNER JOIN

If you want to connect two tables I would use INNER JOIN instead of CROSS JOIN Because the intention of the inner join table is clearer

Upvotes: 0

LoztInSpace
LoztInSpace

Reputation: 5697

They are the same except the second is easier to read, so you should use that one.

Upvotes: 2

Related Questions