Assume we have a table called “Persons” and another table called “Product_Orders”. We will give the table aliases of “p” and “po” respectively.
Now we want to list all the orders that “Ola Hansen” is responsible for.
We use the following SELECT statement:
SELECT po.OrderID, p.LastName, p.FirstName
FROM Persons AS p, Product_Orders AS po
WHERE p.LastName='Hansen' AND p.FirstName='Ola'
Im confused with the OrderID, becouse it is not compared to anything such like.
SELECT po.OrderID, p.LastName, p.FirstName
FROM Persons AS p, Product_Orders AS po
WHERE p.LastName='Hansen' AND p.FirstName='Ola'
AND po.OrderID = p.OrderID.
Am I wrong?
You are right, in your first query, each row from the first table is joined with each row from the second one. Which will result in a result for each order (ignoring whether or not the order belongs to Ola Hansen).
However, I doubt that your table Persons contains forreign keys to orders. A clause such as
would be more sensible, assuming that each row in the Product_Orderds contains a forreign key to the Person that did the order.