database - MYSQL Join 2 tables with 2 relations table -
hi need data database can't figure out how do is...
they have "boutiques" tables , "boutiques_categories" , relation table between them name "boutiques_categories_categories"
here how setup :
boutiques id name 1 x_boutiques2_name 2 y_boutiques_name boutique2 id name 1 x_boutiques2_name 2 y_boutiques2_name boutiques_categories id name 1 x_categorie_name 2 y_categorie_name boutiques_categories_categories boutique_id categorie_id x_boutique_id x_categorie_id y_boutique_id y_categorie_id
i want create sql merge , print csv of data :
export boutiques_id boutique_name boutiques_categories_categorie_name boutiques2_id boutique2_name boutiques_categories_categorie_name
i trying union 2 table
select a.* boutiques union select b.* boutiques2 b
and it's working need join categorie name , can't figure out how it. tried :
select a.* boutiques union select b.* boutiques2 b left join boutiques_categories bc on bc.id = ( select bcc.categorie_id form boutiques_categories_categories bcc bcc.boutique_id = bc.id )
but mysql error can't resolve.
#1242 - subquery returns more 1 row
thank time.
you can union (or union all
, depending on data want) boutiques
, boutique2
derived table, , join boutiques_categories through boutiques_categories_categories.
the table data included in question doesn't seem accurate though. specifically: boutiques_categories_categories containing boutique_id , categorie_id values don't match id columns of associated tables. i'm going assume boutiques_categories_categories have int id values. if not case, please clarify in comments, , can adjust query appropriately.
give query try, , see if returns data expect:
select b.id, b.name, c.name -- boutiques rows unioned tables ( select id, name boutiques union select id, name boutique2 ) b -- join in boutiques_cateogires_categories join boutiques_categories_categories cc on (b.id=cc.boutique_id) -- join in boutiques_categories join boutiques_categories c on (cc.categorie_id=c.id)
Comments
Post a Comment