J1and1
J1and1

Reputation: 920

Need to join 3 tables in SQL

I am a newbie at MySQL..... I am trying to left join 3 tables one contains some_id,name,count,descr and second one has id,some_id,uni_id and the last one has uni_id,price,added,etc So when i try to join these three tables it says that there's no such field named descr

What could be the best way to join these tables without modifying structure of them?

Upvotes: 0

Views: 76

Answers (3)

scottlimmer
scottlimmer

Reputation: 2278

SELECT descr
FROM table1
LEFT JOIN table2 ON table2.some_id = table1.some_id
LEFT JOIN table3 ON table3.uni_id = table2.uni_id

Should do the trick.

Upvotes: 0

David W
David W

Reputation: 10184

It would be ideal if you could post the schema for your tables. Without seeing the query, it sounds like you've made a reference to a field that you may have aliased to the wrong table.

At the most basic level, "descr" doesn't exist as you've tried to reference it, but beyond that, its hard to say without seeing the query itself.

Upvotes: 0

João Silva
João Silva

Reputation: 91299

Assuming the following schema:

  • table1(some_id, name, count, descr), where some_id is the primary key;
  • table2(id, some_id, uni_id), where some_id is a foreign key to table1;
  • table3(uni_id, price, added), where uni_id is a foreign key to table2.

All you need to do is a LEFT OUTER JOIN between the three tables:

SELECT *
FROM table1 t1 LEFT JOIN table2 t2 ON (t1.some_id = t2.some_id)
  LEFT JOIN table3 ON (t2.uni_id = t3.uni_id)

References:

Upvotes: 1

Related Questions