i already submit my problem but without example it was not clear enough so here is the tables :
Users
id_user / key_user
1 / 123FDADA21312QD
2 / 994GFCS4595433D
Companies
id_company / key_company
1 / 3123123123
2 / 5435345454
Products
id_product / id_company_product / code_product
1 / 1 / iphone
2 / 1 / iBook
3 / 1 / macbook
Tyds
id_tyd / user_tyd / product_tyd
1 / 1 / 1 -- mean that the id_user = 1 made a line for the iphone
sold by the company 1
2 / 1 / 2 -- same : user 1 for the ibook of company 1
Here is my request :
SELECT
products.id_product,
users.id_user,
COALESCE(id_tyd, 'NONE') AS id_tyd
FROM tyds
INNER JOIN users
ON tyds.user_tyd = users.id_user
INNER JOIN products
ON tyds.product_tyd = products.id_product
INNER JOIN companies
ON products.id_company_product = companies.id_company
WHERE users.key_user = '123FDADA21312QD'
AND companies.module_key_company = '3123123123'
AND products.cancelled_product >= 0
AND products.code_product = 'iphone'
That will return
products.id_product = 1
users.id_user = 1
tyds.id_tyd = 1
Perfect. But what i’d like to do is getting the users.id_user EVEN IF there is no records for this user in tyds. Like :
SELECT
products.id_product,
users.id_user,
COALESCE(id_tyd, 'NONE') AS id_tyd
FROM tyds
INNER JOIN users
ON tyds.user_tyd = users.id_user
INNER JOIN products
ON tyds.product_tyd = products.id_product
INNER JOIN companies
ON products.id_company_product = companies.id_company
WHERE users.key_user = '123FDADA21312QD'
AND companies.module_key_company = '3123123123'
AND products.cancelled_product >= 0
AND products.code_product = 'macbook'
I’d like to get :
- products.id_product = 3
- users.id_user = 1
- tyds.id_tyd = NONE
Because at this step, i’m sure that the product i ask exists and the user_key exist. But i’m not sure that there is matching records in tyds and that is what i want to test.
Of course, all the tables are simplify and i’ll need more datas but i think this will make my point.
My first move was to put the WHERE condition in the JOIN match but, don’t know why, if i put the products.code_product in the products jointure, it give me tyds records even for different id_product…
Thanks a lot for your help !
if that’s the case, change the join from
INNER JOINtoLEFT JOIN.INNER JOIN, basically returns rows that are present (atleast one match) on all tables being joined, whileLEFT JOINreturns rows on the lefthand side even without matching records on the other tables.Query,