lys916 lys916 - 1 year ago 43
MySQL Question

mysql SELECT WHERE and SELECT again

I have two tables Customers and Customer_orders. I'm trying to SELECT customer's order by using Customer_ID from Customer table.

SELECT * FROM Customer_orders WHERE Customer_ID = SELECT ID FROM Customers WHERE Customer_name = 'John Doe'

This code does not work. How do I do this?


You need to JOIN those 2 tables and then query for what you need. Like this:

SELECT co.* 
FROM Customer_orders co 
INNER JOIN Customers c ON co.Customer_ID = c.ID 
WHERE c.Customer_name = 'John Doe';