Assuming I have these two tables with the following records
cliente
id nombre
1 c1
2 c2
3 c3
4 c4
5 c5
tabla2
id_cli dato
1 d1
3 d3
10 d10
If this selection is made, it shows us the clients that are in table2
SELECT tabla2.*, cliente.nombre
FROM cliente,tabla2
WHERE id = id_cli
id_cli dato nombre
1 d1 c1
3 d3 c3
but what I want is to show me the following result
id_cli dato nombre
1 d1 c1
3 d3 c3
10 d10
Show me the table2 and if the id matches, show the name field
Is this possible in sql? or do I have to do it by code
I think what you are looking for is a
LEFT JOIN
, where the matching fields are displayed, and the unmatched ones from the left table. As explained in the MySQL manual .It should work. Point out that the customer fields that do not match will be shown as
NULL
.Refer to this image to see how the different types of
JOIN
graphics work.Update
It is possible to effectively
NULL
display the fields as blank, as you wish. It's not particularly efficient, but it does what you want. The code is forMySQL
. InMS SQL
, the functionIfNull
is changed toIsNull
, as explained in this question .