1

My question is: Can I do this? I try many things, some of them are:

I need all the info form Table A, and sometimes I need to join this table with Table B. My problem is that when I join both tables, if an specific parameter in Table A is not in Table B just give me the records when the specific parameter are, but I want all the records.

Table A

|--------------------------------|
|           Table A              |
|-------------------|------------|
| id_table_A | name | id_table_B |
|------------|------|------------|
|   1        | Joe  |  1         |
|------------|------|------------|
|   2        | Ben  |            |
|------------|------|------------|
|   3        | Lya  |            |
|------------|------|------------|
|   4        | luis |  2         |
|------------|------|------------|

Table B

|----------------------|
|      Table B         |
|----------------------|
| id_table_B | Elements|
|------------|---------|
|   1        |    Car  | 
|------------|---------|
|   2        |    Byke | 
|------------|---------|
|   3        |    Moto | 
|------------|---------|

What I want to show in my View is this:

|------------|------|------------|
| id_table_A | name | Elements   |
|------------|------|------------|
|   1        | Joe  |  Car       |
|------------|------|------------|
|   2        | Ben  |            |
|------------|------|------------|
|   3        | Lya  |            |
|------------|------|------------|
|   4        | luis |  Byke      |
|------------|------|------------|

My model In my model this is what I tried:

"SELECT * FROM table_A, table_B where table_A.id_table_B = table_B.id_table_B"

But this query only show me data 1 and 4. This can be done or not? Thanks in advance.

Mary
  • 197
  • 1
  • 15

2 Answers2

3

You can use left join

SELECT * 
FROM table_A
left join table_B on table_A.id_table_B = table_B.id_table_B

Left join is used when the keys between tables may not always match. In this case, the left join retrieves the correct match where it's possible and the values become NULL when not possible.

SQL LEFT JOIN Documentation

GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

You need an explicit LEFT JOIN as opposed to the implicit INNER JOIN that is used when you simply list the tables like that. https://www.w3schools.com/sql/sql_join_left.asp

Marc DePoe
  • 43
  • 6