Jump to content

Left Join Error


mattclements

Recommended Posts

Hey all,

This used to work, and with a change from MySQL 4 to MySQL 5 it broke - I belive it is a bug currently being fixed:

select prod_id, prod_name, prod_cat, cat_name, man_name, prod_price, prod_date, prod_seq, prod_default, type_name from prods, cats, mans left join types on prod_type=type_id where prod_cat=cat_id and prod_man=man_id order by cat_seq, cat_name, prod_seq, prod_name

 

causes:

Unknown column 'prod_type' in 'on clause'

 

Any idea how to rewrite the code to fix this?

 

Regards,

Matt

Link to comment
https://forums.phpfreaks.com/topic/188330-left-join-error/
Share on other sites

prod_type exists in the prods table.

 

Hint: use aliases for greater readability of your queries - Please explain

Hint2: avoid joins through comma. Better use INNER JOIN - as far as im aware I am not joining through comma

Link to comment
https://forums.phpfreaks.com/topic/188330-left-join-error/#findComment-994237
Share on other sites

So - My updated query is:

select p.prod_id, p.prod_name, p.prod_cat, c.cat_name, m.man_name, p.prod_price, p.prod_date, p.prod_seq, p.prod_default, t.type_name
from prods AS p, cats AS c, mans AS m
left join types AS t on p.prod_type=t.type_id
where prod_cat=cat_id
and prod_man=man_id
order by cat_seq, cat_name, prod_seq, prod_name

 

Still causing:

Unknown column 'p.prod_type' in 'on clause' (Error 1054)

 

How can I avoid using commas?

 

Regards,

Matt

Link to comment
https://forums.phpfreaks.com/topic/188330-left-join-error/#findComment-994250
Share on other sites

select p.prod_id, p.prod_name, p.prod_cat, c.cat_name, m.man_name, p.prod_price, p.prod_date, p.prod_seq, p.prod_default, t.type_name
from (prods AS p, cats AS c, mans AS m)
left join types AS t on p.prod_type=t.type_id
where prod_cat=cat_id
and prod_man=man_id
order by cat_seq, cat_name, prod_seq, prod_name

 

Fixed!

 

Cheers,

Matt

Link to comment
https://forums.phpfreaks.com/topic/188330-left-join-error/#findComment-994254
Share on other sites

from (prods AS p, cats AS c, mans AS m)
left join types AS t on p.prod_type=t.type_id
where prod_cat=cat_id
and prod_man=man_id

 

is equivalent to

 

FROM
  prods AS p
INNER JOIN
  mans AS m
ON 
  m.man_id = p.prod_man
INNER JOIN
  cats AS c
ON
  c.cat_id = p.prod_cat
LEFT JOIN
  types AS t
ON
  t.type_id = p.prod_type

 

This way MySQL will know exactly which tables you want to join and on what conditions.

 

Link to comment
https://forums.phpfreaks.com/topic/188330-left-join-error/#findComment-994266
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.