Dutchdeveloper
Dutchdeveloper

Reputation: 67

{SQL PHP} Help by building query

Maybe a stupid question, but i am in a bit of a rush. I am asked to change some php files, which is done, but i am stuck on the query needed to make it fully work.

I have the next 2 tables named: Orders and Customers

I use the following query to display all information from the orders table:

SELECT * FROM Orders WHERE ORD_ID="$ID";

This displays something like this:

Order number = 1
object = Testobject
Customer = 256

now i want customer to display with their name, like Dutch Developer instead of 256

The table Customer looks like this:

ID Name

So The Customer number in the table orders should be linked to the id in Customer and then the ID in customer should be linked to the Name. And then it should display only the name.

Maybe it is a very easy thing, but my sql knowledge is not that good.

Thanks in advance

Upvotes: 0

Views: 62

Answers (3)

sagi
sagi

Reputation: 40481

You can also use a correlated query:

SELECT o.* ,
       (SELECT c.customer_name FROM customers c
        WHERE c.id = o.customer) as cust_name 
FROM orders o

This is for all orders, if you want it for a specific one then add the WHERE clause back

WHERE ORD_ID="$ID";

Upvotes: 1

J. Zend
J. Zend

Reputation: 98

try this :

select b.name from Orders a,Customers b where a.number=b.id ;

Upvotes: 0

Robin
Robin

Reputation: 23

Try a JOIN

SELECT * 
FROM Orders 
JOIN Customers ON Customers.ID = Orders.Customer
WHERE ORD_ID="$ID";

The JOIN say's add this table WHERE Customer equals his Customers.ID

Upvotes: 2

Related Questions