简体   繁体   中英

Select every value (even when it is null) with inner join sql

This is my sql select statment

SELECT k.name, c.name AS nameCustomer, o.*
                                        FROM offertes o
                                        INNER JOIN customers k
                                        ON o.idCustomer= k.id
                                        INNER JOIN contactperson c
                                        ON o.idContact = c.id;

When o.idContact doesn't exist, then there will be no values selected. I wan't to get NULL instead of nothing. It still need to SELECT the whole row! Can anyone help me?

ps. I think it's going wrong with the inner join ( ON o.idContact = c.id );

Try this:

Replace your last INNER JOIN with LEFT JOIN .

Using LEFT JOIN you tell my main table ( offertes ) returns always result but if in secondary table ( contactperson ) there's no row matches returns NULL all fields of that table

SELECT k.name, c.name AS nameCustomer, o.*
FROM offertes o
INNER JOIN customers k
ON o.idCustomer= k.id
LEFT JOIN contactperson c
ON o.idContact = c.id;

You'll need an outer join instead. eg

SELECT k.name, c.name AS nameCustomer, o.*
  FROM customers k LEFT OUTER JOIN 
       (offertes o INNER JOIN 
        contactperson c
        ON o.idContact = c.id)
       ON o.idCustomer= k.id;

(I assume there'll be a counterpart of an offerte in contactperson, else you'll need an outer join there as well)

you need to rewrite a query using outer join

SELECT k.name, c.name AS nameCustomer, o.*
  FROM customers k
  LEFT OUTER JOIN offertes o ON o.idCustomer = k.id
  LEFT OUTER JOIN contactperson c ON o.idContact = c.id;                                        

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM