Skip to content
Advertisement

How to join two tables in sql according to a determined value?

I have the following tables and I have to get all orders of customer Meier.

Table Customer with columns cid and name:

cid           name
-------------------------
13            M. Mueller
17            A. Meier
23            I. Schulze

Table Orders with columns Oid, Did, Date and Cid:

Oid       Did       Date          Cid
--------------------------------------
3         7        2002-12-01     17
5         11       2003-04-27     23
7         5        2003-05-13     17
10        5        2003-09-01     13

What I have tried is the following:

SELECT Oid.Orders, Did.Orders, Date.Orders, Cid.Orders,
FROM Orders
INNER JOIN Customer ON Cid.Orders = cid.Customer
WHERE name.Customer = "A. Meier"

But there is a syntax error that I am not able to find. How should I proceed in this case?

enter image description here

Advertisement

Answer

The best way is to use aliases. So your select statement should look like this(I will use alias o for Orders and c for Customer):

 SELECT * from Orders o inner join Customer c on c.cid=o.Cid where c.name='A. 
 Meier'

If you would like to solve the problem without aliases (which I don’t prefer), you should do this:

 SELECT Orders.Oid, Orders.Did, Orders.Date, Orders.Cid,
 FROM Orders
 inner JOIN Customer ON Orders.Cid = Customer.cid
 where Customer.name = 'A. Meier'

So table name before table attribute. And be careful around the quotes… ” and ‘ have different meaning… It also leads in errors. Please read this post What is the difference between single and double quotes in SQL?

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement