Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.