Misiur
Misiur

Reputation: 5297

Fight against INNER JOINS - Chapter: unknown column

SELECT p.id
FROM produkty p, przyporzadkowania pr, stany_magazynowe, gk_grupy_produkty
INNER JOIN sub_subkategorie ssi
ON pr.sub_subkategorie_id = ssi.ID

Tables and their important fields

produkty - id, pozycja
przyporzadkowania - id, produkt_id, sub_kategoria_id, sub_subkategoria_id
sub_subkategorie - id, subkategorie_id, pozycja
subkategorie - id, kategorie_id, pozycja
kategorie - id, pozycja

Error "#1054 - Unknown column 'pr.sub_subkategorie_id' in 'on clause'"

Tried with

SELECT p.id, pr.sub_subkategorie_id

Same result.

Full query (not tested due to failure of query above):

SELECT p.id
FROM produkty p, przyporzadkowania pr, stany_magazynowe, gk_grupy_produkty
INNER JOIN sub_subkategorie ssi ON pr.sub_subkategorie_id = ssi.ID
INNER JOIN subkategorie si ON ssi.subkategorie_id = si.id
INNER JOIN kategorie c ON si.kategorie_id = c.id
WHERE stany_magazynowe.produkty_id = p.id
AND p.id = pr.produkty_id
AND pr.sub_subkategorie_id =1
AND p.widoczny = '1'
AND p.id = gk_grupy_produkty.id_produktu
AND gk_grupy_produkty.id_grupy =1
AND gk_grupy_produkty.towar_widocznosc =1
AND c.id = '1'
ORDER BY c.pozycja, si.pozycja, ssi.pozycja, p.pozycja

Hope that I gave enough info (earlier question - SELECT * FROM table WHERE field IN (SELECT id FROM table ORDER BY field2))

EDIT:

Yes, there is typo, but only here, on stackoverflow (too much coffee, my fingers are flying). Thank you all, You saved my day!

Upvotes: 1

Views: 1144

Answers (4)

Mark Byers
Mark Byers

Reputation: 838696

You're joining the tables in the wrong order:

SELECT p.id
FROM produkty p, stany_magazynowe, gk_grupy_produkty, przyporzadkowania pr
INNER JOIN sub_subkategorie ssi
ON pr.sub_subkategorie_id = ssi.ID

The error is due to the higher precedence of the JOIN keyword compared to the comma. Errors like this are one of the reasons why I would urge you not to use the implicit join syntax with the comma and instead always write your joins explicitly using the JOIN keyword.

Here is your complete query rewritten using explicit joins:

SELECT p.id
FROM produkty p
INNER JOIN przyporzadkowania pr ON p.id = pr.produkty_id
INNER JOIN stany_magazynowe ON stany_magazynowe.produkty_id = p.id
INNER JOIN gk_grupy_produkty ON p.id = gk_grupy_produkty.id_produktu
INNER JOIN sub_subkategorie ssi ON pr.sub_subkategorie_id = ssi.ID
INNER JOIN subkategorie si ON ssi.subkategorie_id = si.id
INNER JOIN kategorie c ON si.kategorie_id = c.id
WHERE pr.sub_subkategorie_id = 1
AND p.widoczny = '1'
AND gk_grupy_produkty.id_grupy =1
AND gk_grupy_produkty.towar_widocznosc =1
AND c.id = '1'
ORDER BY c.pozycja, si.pozycja, ssi.pozycja, p.pozycja

Related question

Upvotes: 6

Paul Sasik
Paul Sasik

Reputation: 81509

Start with this: (fill in the ???)

SELECT p.id
FROM produkty p
JOIN przyporzadkowania pr ON p.??? = pr.???
JOIN sub_subkategorie ssi ON pr.sub_subkategorie_id = ssi.ID

(You don't need to specify INNER)

Your original FROM clause:

FROM produkty p, przyporzadkowania pr, stany_magazynowe, gk_grupy_produkty

Has tables p and pr for which no JOIN condition is specified which will result in a cartesian product or cross join (probably not a good thing in this case) as well as tables that aren't referenced anywhere else in your query:

stany_magazynowe, gk_grupy_produkty

Specify a JOIN for

produkty p AND przyporzadkowania pr

and remove

stany_magazynowe, gk_grupy_produkty

Hint: If you do not reference any columns from a table in your SELECT, ORDER BY, GROUP BY, WHERE then the table probably does not belong in your FROM clause. (Unless it's a join/junction table.)

Upvotes: 0

Guffa
Guffa

Reputation: 700562

You are mixing "classical" joins with the join keyword. You should use the join keyword for all the joins.

The error comes from that you are joining on the gk_grupy_produkty table, where that field doesn't exist. The database really looks at your query as:

SELECT p.id
FROM
  produkty p,
  przyporzadkowania pr,
  stany_magazynowe,
  (gk_grupy_produkty INNER JOIN sub_subkategorie ssi ON pr.sub_subkategorie_id = ssi.ID)

You should use:

SELECT p.id
FROM
  produkty p
  INNER JOIN przyporzadkowania pr ON p.id = pr.produkty_id
  INNER JOIN stany_magazynowe ON stany_magazynowe.produkty_id = p.id
  INNER JOIN gk_grupy_produkty ON p.id = gk_grupy_produkty.id_produktu
  INNER JOIN sub_subkategorie ssi ON pr.sub_subkategorie_id = ssi.ID
  INNER JOIN subkategorie si ON ssi.subkategorie_id = si.id
  INNER JOIN kategorie c ON si.kategorie_id = c.id
WHERE
  pr.sub_subkategorie_id = 1 AND
  p.widoczny = '1' AND
  gk_grupy_produkty.id_grupy = 1 AND
  gk_grupy_produkty.towar_widocznosc = 1 AND
  c.id = '1'
ORDER BY c.pozycja, si.pozycja, ssi.pozycja, p.pozycja

Upvotes: 1

CtrlDot
CtrlDot

Reputation: 3122

Is the column named sub_subkategoria_id a typo?

Upvotes: 0

Related Questions