jchi2241
jchi2241

Reputation: 2236

MySQL - Selecting data from three different tables

I'm trying to select data from three different tables. How would I go about joining these tables to make the proper query?

****customers****
--------------------------
id            full_name
54            Matt Damon
53            Jimmy Kimmel

****samples****
--------------------------
id            rma_id
57            USARP011315-25
56            USARP011315-24

****early_ships****
--------------------------
customer_id   sample_id   shipping_carrier
54            57          UPS
53            56          FedEx

This is the query I've been running but I've yielded 0 results. This is the tutorial I've been following: http://javarevisited.blogspot.com/2012/11/how-to-join-three-tables-in-sql-query-mysql-sqlserver.html

SELECT samples.rma_id, customers.full_name, early_ships.shipping_carrier, 
FROM customers c JOIN early_ships e ON c.id = e.customer_id
JOIN samples s ON e.sample_id = s.id

Upvotes: 0

Views: 88

Answers (2)

su8898
su8898

Reputation: 1713

You have to use the table aliases in the SELECT clause. Please check this fiddle I created for you. It's working perfectly. http://sqlfiddle.com/#!2/49462/8

Upvotes: 2

bennettp123
bennettp123

Reputation: 419

Two issues:

  1. There's an extra comma at the end of the SELECT clause.
  2. In the FROM..JOIN clause, you've given nicknames, but then you're trying to use the original names in the SELECT clause.

Try this:

SELECT s.rma_id, c.full_name, e.shipping_carrier

FROM customers c
JOIN early_ships e
    ON c.id = e.customer_id
JOIN samples s
    ON e.sample_id = s.id

(whitespace added for readability)

Upvotes: 1

Related Questions