Relationship between tables in SELECT - SQL SERVER

Asked

Viewed 426 times

1

I’m having a question on how to add a relationship between tables.

Example: I am using this command to get the data. However, it returns me with the ID’s of the other tables. I would like to return the names related to the ids.

SELECT * FROM BASE_MARCACAO
WHERE MA_DATA='2019-02-25 00:00:00.000' AND MA_CLINICA='1' and MA_PROFISSIONAL='6771' and MA_CLIENTE_CONVENIO IS NOT NULL
  • you have to go into more detail about your question, it’s very vague.

1 answer

1

You can solve this through a INNER JOIN.

Following example:

SELECT bm.MA_DATA as data, 
       bm.MA_CLINICA as clinica, 
       bm.MA_PROFISSIONAL as profissional, 
       ot.NOME as nomeOutraTabela
FROM BASE_MARCACAO bm
INNER JOIN OUTRA_TABELA ot
ON bm.id = ot.id
WHERE bm.MA_DATA='2019-02-25 00:00:00.000' AND bm.MA_CLINICA='1' and bm.MA_PROFISSIONAL='6771' and bm.MA_CLIENTE_CONVENIO IS NOT NULL

Just making an observation to better understand the answer: OUTRA_TABELA would be the table in which the names referring to the respective ids would be.

Something else, bm and ot are aliases of their respective tables, clinica, data, profissional e nomeOutraTabela are alias of the columns of each table. It is good practice to always use to make the code more readable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.