-2

I have a MySQL database with 2 tables:

Table A:
id     name   age
123a   John   34
143w   Mark   27
143x   Rony   30

Table B:
id     company     job
143w   Google      developer


I need:
id     name   age  company  job
123a   John   34
143w   Mark   27   Google   developer
143x   Rony   30

I need a select statement that can extract the result above.
Thanks in advance
Paulo

  • 2
    You need a `LEFT JOIN` on `id` – Nick Feb 25 '20 at 03:01
  • Hi, thanks. I tried that. I tested that one already but for records on A that have no corresponding records on B instead, fields that should be empty get the content of other records. in my model case above running a LEFT JOIN statement I would get: For id=123a name=John age=34 company=Google job=developer and the same happens to id=143x when company and job for those two should be blank. Something is still missing. Thanks – Paulo Borges Feb 25 '20 at 10:58
  • 1
    http://www.sqlfiddle.com/#!9/ce54c9/3 shows a left join giving the results you want. You might want to `COALESCE` the `NULL` values into empty strings e.g. http://www.sqlfiddle.com/#!9/ce54c9/4 – Nick Feb 25 '20 at 13:19
  • Hi, thanks, I've got it working the way I need with the left join. Thanks for the contribution. – Paulo Borges Feb 25 '20 at 14:02

1 Answers1

0

from the requirements that you need, i assume you need this query :

SELECT *
FROM A
INNER JOIN B ON A.id = B.id;
18Man
  • 572
  • 5
  • 17
  • Dear Fachry, thanks, but there is a problem. I tested your sugestion but for records on A that have no corresponding records on B instead on blank fields the get the content of other records. in my model case above running your SELECT statement I would get: For id=123a name=John age=34 company=Google job=developer and the same happens to id=143x when company and job for those two should be blank. Something is still missing. Thanks – Paulo Borges Feb 25 '20 at 10:44