Jump to content

PostgreSQL SELECT


NCS_One

Recommended Posts

Boas.

É possivel no mesmo "select" fazer um query a uma tabela e com um campo que é

retornado fazer um query a outra tabela ?

Ex:

Table1 tem : user_id e user_name.

Table2 tem : table_id e user_id.

Fazer um query à Table2 para retornar o user_id e com esse user_id ir à Table1 buscar o

user_name de quem tem o user_id igual ao que foi retornado pela Table2.

Agradeço toda a ajuda.

Se a vida te voltar as costas aproveita e apalpa-lhe o cu.

Link to comment
Share on other sites

Seria qualquer coisa como isto ?

SELECT Table2.user_id, Table1.user_name FROM Table2 WHERE Table2.table_id = 1
             INNER JOIN Table1 ON (Table1.user_id = Table2.user_id);

ou :

SELECT Table2.user_id, Table1.user_name FROM Table2, Table1
             WHERE Table2.table_id = 1 AND Table1.user_id = Table2.user_id;

Se a vida te voltar as costas aproveita e apalpa-lhe o cu.

Link to comment
Share on other sites

O que queres é tipo a primeira hipotese mas tens o WHERE no sitio incorrecto. O WHERE vem depois dos JOINs.

SELECT Table2.user_id
     , Table1.user_name 
FROM Table2 INNER JOIN Table1 
    ON (Table1.user_id = Table2.user_id)
WHERE Table2.table_id = 1;

"Give a man a fish and he will eat for a day; Teach a man to fish and he will eat for a lifetime. The moral? READ THE MANUAL !"

Sign on a computer system consultant's desk

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.