0

I have the following two tables:

projects_expended:

+---------------+------------+----------+
| project_name  | project_id | Utilised |
+---------------+------------+----------+
| CStreamTax    |          1 |     3500 |
| CStreamTravel |          2 |      149 |
+---------------+------------+----------+

projects:

+------------+---------------------+
| project_id | project_budgetstart |
+------------+---------------------+
|          1 |               15000 |
|          2 |               15000 |
|          3 |                 500 |
+------------+---------------------+

I would like to make a joint table of the two, but with the Utilised field filled with NULL when a project_id form the projects table does not match one of the entries in the projects_expended table.

My basic attempt was as follows:

SELECT projects.*, projects_expended.utilised
  FROM projects_expended
  JOIN projects
    ON projects.project_id = projects_expended.project_id;

And the result:

+------------+---------------------+----------+
| project_id | project_budgetstart | utilised |
+------------+---------------------+----------+
|          1 |               15000 |     3500 |
|          2 |               15000 |      149 |
+------------+---------------------+----------+

I understand why I get the result I get. But I'm pretty stuck with this. I've made other attempts (too many to count).

Jens
  • 67,715
  • 15
  • 98
  • 113

2 Answers2

0

Use left outer join:

SELECT projects.*, projects_expended.utilised
  FROM projects
  LEFT OUTER JOIN projects_expended

    ON projects.project_id = projects_expended.project_id;
Jens
  • 67,715
  • 15
  • 98
  • 113
0

I found my answer from:

LEFT JOIN vs. LEFT OUTER JOIN in SQL Server

It was pretty helpful.

The solution:

SELECT projects.*, projects_expended.utilised
  FROM projects_expended
 RIGHT JOIN projects
    ON projects.project_id = projects_expended.project_id;
Community
  • 1
  • 1