Adding entries from multiple MySQL tables using one single SQL join statement, but only if there are entries available in the second table -
using 1 single sql query join: how can add entries second table if there corresponding entry available?
project source description | source source_id | value ---------------------------- -------------------------------- project 1 | 1 1 | additional info 1 project 2 | null
when type
select project.description, source.value project, source project.source = source.source_id , project.description = "project 1";
as desired receive
project 1 | additional info 1
however when replace project 1
project 2
in last line, won't result, because project.source
null
. possible use single sql query outputs this?
project 2 | null
i´m looking query covers both cases.
any ideas?
you can use left join
on project
table make sure all projects appear in result set if have no matching value
in source
table. projects project
table not match have null
value
.
select project.description description, source.value value project left join source on project.source = source.source_id
output:
+--------------+--------------------+ | description | value | ---------------+--------------------+ | project 1 | additional info 1 | | project 2 | null | +--------------+--------------------+
Comments
Post a Comment