INNER and LEFT JOIN on mysql -
i have needing chain joins queries, , not able result want.
have many tables save product info:
-products: name , description in many languages.
-products_attributes: common attributes of products provider or buying price.
-products_locations: info concerning locations sell product, stock, or selling price.
-other important table companies one: of companies provider.
well, query:
select p.id , p.name nameproduct , p.reference refproduct , a.buy_price priceproduct , l.active , l.sell_price , l.stock stockproduct , c.title p_name products p left join products_location l on l.product_reference = p.reference , l.active = 1 , l.location_id = 4 join products_attributes on a.product_reference = p.reference , p.lang = 'es' , a.provider = 6 join companies c on a.provider = c.id , c.id = 6
what want products of provider, if location executing query has product, row of result must return concerning @ product<->location, in contrary must return null in column related relation.
@ moment, query, getting products of provider has relation between product<->location (through products_location table).
any way that??
thank you.
edit
an example trying this:
table: companies
id | title 1 | somename 6 | providername
table: products
id | reference | name | lang 1 | 11111 | 1_es | es 2 | 11111 | 1_en | en 3 | 22222 | 2nam | es 4 | 33333 | 3nam | es 5 | 44444 | 4nam | es 6 | 55555 | 5nam | es
table: products_atributte
id | product_reference | buy_price | provider 1 | 11111 | 10 | 6 1 | 22222 | 15 | 6 1 | 33333 | 20 | 6 1 | 44444 | 12 | 1 1 | 55555 | 13 | 1
table: products_locations
id | product_reference | location_id | sell_price | stock | active 1 | 11111 | 4 | 26 | 10 | 1 1 | 11111 | 5 | 25 | 13 | 1 1 | 22222 | 5 | 20 | 13 | 1 1 | 44444 | 5 | 21 | 1 | 1 1 | 55555 | 5 | 22 | 2 | 1
and result must this:
nameproduct | refproduct | priceproduct | active | sell_price | stockproduct | p_name 1_es | 11111 | 10 | 1 | 26 | 10 | providername 2nam | 22222 | 15 | null | null | null | providername 3nam | 33333 | 20 | null | null | null | providername
if use left join in products_locations table, don´t 2 last rows, , if use left join tables duplicates product references, products provided other providers (in example 1-> somename).
you correct left join
products
, products_location
tables. however, used inner join
other 2 tables in query , believe may reason why seeing records have relation between product , location. logic product not have location also not have entry in, example, products_attributes
table. hence, non matching records want retain filtered off downstream inner join
. remedy this, use left join
everywhere:
select products.id, products.name nameproduct, products.reference refproduct, products_attributesbuy_price priceproduct, products_location.active, products_location.sell_price, products_location.stock stockproduct, provider.title p_name products left join products_location on products_location.product_reference = products.reference , products_location.active = 1 , products_location.location_id = 4 left join products_attributes on products_attributes.product_reference = products.reference , products.lang = 'es' , products_attributes.provider = 6 left join companies provider on products_attributes.provider = provider.id , provider.id = 6
Comments
Post a Comment