join – Not unique table/alias in MySQL

join – Not unique table/alias in MySQL

After table name or field name you can give alias name and you can use it.

n

SELECT C.name, C.sub_name, PT.namenFROM categories CnINNER JOIN presentations PnON C.id=P.categorynINNER JOIN parts PTnON PT.id=P.partsnWHERE P.id=5;n

n

In above example C,PT,P is alias name of categories,presentation,parts table respectively.

You are joining the same table 2 times and hence you need to provide the unique alias name. However looks like you are looking for parts table and hence need to join that table

n

SELECT ncategories.name, ncategories.sub_name, nparts.namenFROM categoriesnINNER JOIN presentations ON categories.id=presentations.categorynINNER JOIN parts ON parts.id=presentations.partsnWHERE presentations.id=5;n

n

For better readability you can always give some short alias name something as

n

selectnc.name,nc.sub_name,np.name as parts_namenfrom categories cnjoin presentations pr on pr.category = c.idnjoin parts p on p.id = pr.partsnwhere pr.id = 5n

join – Not unique table/alias in MySQL

Leave a Reply

Your email address will not be published.