Skip to content

Instantly share code, notes, and snippets.

@dan81989
Created May 23, 2016 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dan81989/08d3d24302611c452e8d13b9c5b26ea8 to your computer and use it in GitHub Desktop.
Save dan81989/08d3d24302611c452e8d13b9c5b26ea8 to your computer and use it in GitHub Desktop.
Let’s say we want to find all orders placed by a particular customer. We can do this by joining the customers and orders tables together using the relationship established by the customer_id key.
select order_date, order_amount
from customers
join orders
on customers.customer_id = orders.customer_id
where customer_id = 3
@daniel-schroeder-dev
Copy link

I created the customers and orders tables in MySQL 5.7 in order to follow along with your tutorial. When I tried the above command I got two errors:

ERROR 1054 (42S22): Unknown column 'order_amount' in 'field list'
ERROR 1052 (23000): Column 'customer_id' in where clause is ambiguous

The first was easily fixed by changing order_amount to amount, since that was the name of the column in the orders table.

The second I had to dig around for, and to reproduce the results shown on the webpage I found this query to be satisfactory:

SELECT order_id, order_date, amount
FROM orders
JOIN customers
ON customers.customer_id = orders.customer_id
WHERE orders.customer_id = 3;

Is the query posted on the webpage a typo, or is there just some difference between how the JOIN command is run on different SQL implementations?

@rebwill
Copy link

rebwill commented Mar 14, 2020

To echo the above comment, I believe there is a typo in this query. In order to have order_id included in the results as shown, the query should be:

SELECT order_id, order_date, order_amount
FROM customers
JOIN orders
ON customers.customer_id = orders.customer_id
WHERE customer_id = 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment