Aaron
Aaron

Reputation: 41

mySQL multiple inner joins

I have two tables I'm pulling information from.

Lets say table1 has following columns (id, title, category, sub_category, sub_sub_category)

Lets say table2 has following columns (category_id, category_name)

I have a select statement that so far looks as follows:

SELECT
  table1.id,
  table1.title,
  table2.category_name as Cat1,
  table2.category_name as Cat2,
  table2.category_name as Cat3
FROM
  table1,
  table2
INNER JOIN table2 as c1 ON c1.category_id = table1.category
INNER JOIN table2 as c2 ON c2.category_id = table1.sub_category
INNER JOIN table2 as c3 ON c3.category_id = table1.sub_sub_category
WHERE
  table1.id = ?

This gives me an error about table1.category being an unknown column

I have also tried

SELECT
  table1.id,
  table1.title,
  table2.category_name as Cat1,
  table2.category_name as Cat2,
  table2.category_name as Cat3
FROM
  table1,
  table2
WHERE table1.id = ?
AND   table1.category = table2.category_id
AND   table1.sub_category = table2.category_id
AND   table1.sub_sub_category = table2.category_id

The last example at least gives me column output I'm looking for which would be

(table1.id, table1.title, table1.category name, table1.sub_category name...)

So showing the category name from table 2 instead of the ID's. I am an amateur coder and haven't had to use inner joins before but maybe that is what I need to do. I just can't figure out how to get it to output the data I need.

Thank you in advance for your time and consideration.

Upvotes: 2

Views: 7357

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1271151

Your problem is that you have a comma in the from clause. Simple rule: never use commas in the from clause. Always use explicit join syntax.

Then, you also have table2 mentioned an extra time, and your select is pulling columns from the wrong instance of table2.

The fixed up query looks like:

SELECT t1.id, t1.title,
       c1.category_name as Cat1, c2.category_name as Cat2,
       c3.category_name as Cat3
FROM table1 t1 INNER JOIN
     table2 c1
     ON c1.category_id = t1.category INNER JOIN
     table2 c2
     ON c2.category_id = t1.sub_category INNER JOIN
     table2 c3
     ON c3.category_id = t1.sub_sub_category
WHERE t1.id = ?;

Upvotes: 4

Aleksandar Miladinovic
Aleksandar Miladinovic

Reputation: 1027

Ups, you have a couple of problems here. First table2 can have only one column called category_name so there is no reason to select it three times like you do, result of that would be that you have three absolutely same column with different name.

Second the syntax is completely wrong. Syntax for INNER JOIN is

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;

also you need to have column in both table usually foreign key and that column will be use to match data. I guess in your case it should be category_id from second table and in first table that's column you named category (it, should be renamed at category_id it's more convenient)...

So if understand right you want to select id, title from first table and category_name from second you could do that like this:

SELECT table1.id, table1.title, table2.category_name
FROM table1
INNER JOIN table2
ON table1.category_id = table2.category_id;

And friendly advice to you is to find some online sources and learn a little bit more about this before you continue with what ever you do...

Upvotes: -1

Related Questions