Question

how do I select specific columns from three different tables based on their primary key. I have three tables,

Table1 orders (orderID, CustomerID, productID, orderdate, ,order quantity order_description).
Table2 Customers(CustomerID, Fname, Lname, Email).
Table3 Products(ProductID, Productname, Product weight).

I would like a query where I can display a result for a specific order for a specific customer and only the product he orderred. so I would like a new result to display

Customer Fname, lname, product name, quantity, for based on the specific orderID. Thank you. Hopefully you understood my question, Sorry my English is very bad.

Était-ce utile?

La solution

Try this one out:-

SELECT 
    c.Fname, c.Lname, p.productname, o.quantity
FROM Orders o
JOIN Customers c on o.CustomerID = c.CustomerID
JOIN Products p on p.ProductID = o.ProductionID
WHERE o.OrderID = @OrderID

Autres conseils

$sql = "SELECT c.Fname, c.Lname, p.Productname, o.orderquantity
FROM orders o
    JOIN Customers c ON o.CustomerID = c.CustomerID
    JOIN Products p ON o.productID = p.ProductID
WHERE o.orderID = '$orderID';";

Doing direct variable injection like that is BAD, but without knowing what db connection you're using, that should be enough to get you going in the right direction.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top