1

There are my database

user
id | user_code | user_name
1  | 1x2d       | Peter

shops
id | shop_code | shops_name
1  | 1234      | shop1
2  | 3456      | shop2

articles
id | art_code | art_name
1  | 5678     | computer
2  | 0987     | display

action
id | M_art_code | M_shop_code | M_user_code
1 | 5678        | 1234        | 1x2d
2 | 0987        |             | 1x2d

I would like to SELECT all value of action where .... with all information from other table

Result
M_art_code | M_shop_code | shops.shops_name | M_user_code| user.user_name
5678       | 1234        | shop1            | 1x2d       | Peter
0987       |             |                  | 1x2d       | Peter

I try with

SELECT * FROM user U, shops S, articles A, action AC 
WHERE AC.M_user_code = U.user_code
AND AC.M_shop_code = S.shops_code AND M_art_code LIKE %%

But with this SELECT I've just row ths all value not empty. I need all value. How I can do to have I would like?

PS: sorry for my English...I'm french

2

Instead of the (implicit) inner joins you have to use left joins if you want all rows from the left table regardless of the existence of a matching row from the right table.

SELECT ac.m_art_code,
       ac.m_shop_code,
       sh.shops_name,
       ac.m_user_code,
       us.user_name
       FROM action ac
            LEFT JOIN shops sh
                      ON sh.shop_code = ac.m_shop_code
            LEFT JOIN user us
                      ON us.user_code = ac.m_user_code;
| improve this answer | |
  • USING () is nicer there. – Evan Carroll Jan 15 '19 at 5:39
  • @EvanCarroll USING() needs the field names to be absolutely equal... – Akina Jan 15 '19 at 7:59
  • Thx sticky bit :) – Stolker Jan 15 '19 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.