Home > other >  Inside a select, just select a row from another table where one column is equal to something
Inside a select, just select a row from another table where one column is equal to something

Time:01-17

I have a select query working fine, but one information is in another table, but this another table is crazy.

crazy table

In this table, the info a need is only where the column is equal to CPF

But I can't do this query like usually

My query

select
 name,
 date,
 CPF
from user

Yes, there is a primary key between the two tables, codintfunc

But how I select CPF from the other table if theres 2 condition and inside the select query I can't do this.

PS: sorry my bad english, I'm Brazilian

I've tried select inside a select, joins and where, but no lucky

More Info

Because the database has sensitive information, e due to LGPD from Brazil, I made some visual sheet to help understand what I need.

I'm using 3 tables to acquire all data I need in this query

table1

table2

table3

What I need from result is

wanted_result

CodePudding user response:

You can join on cod and add additional conditions to filter only the typedoc you need:

SELECT t1.cod, t1.name, t2.typedoc, t2.numdoc, t3.mat_escocial
FROM   table_1 t1
JOIN   table_2 t2 ON t1.cod = t2.cod AND t2.typedoc = 'cpf'
JOIN   table_3 t2 ON t1.cod = t3.cod
  • Related