Thread: How To Use JOIN?

How To Use JOIN?

From
Takashi Tokunaga
Date:
Hello PGSQL users, I'm having trobule using join statement:

I've created two tables and trying to compare contents of two
tables and receive results which found on two tables equaled.

------Here's what I did ------

1. I create two tables:

  create table test1 (
    name_1  varchar(10),
    code_1 int
  )\g

  create table test2 (
   name_2  varchar(10),
   code_2 int
  )\g

2. Entered sample data:

  insert into test1 values ( '10', 1) \g
  insert into test1 values ( '11', 2) \g

  insert into test2 values ( '11', 1) \g
  insert into test2 values ( '11', 2) \g

3. Now after that worked without any problem, so I now create SELECT
   statement:

  select * from test1 test2 where code_1 = test.code_2 \g
  ERROR:  attribute 'code_2' not found

nope that didn't work

  select test1.name_1 from test1 test2 where test1.code_1 = test2.code_2 \g
  ERROR:  attribute 'code_2' not found

Umm... still no change.

------

Mmm... Any ideas?
If I can get value of test1.name_1 which also had equal value on code_1
and test2.code_2, I'll solve my trouble.  It shouldn't be that complex but it's
been driving me nuts for few hours.

I'm using postgresql-6.3.2 under FreeBSD 2.2.6.

Thanx in Advance,

Takashi



Re: [SQL] How To Use JOIN?

From
Herouth Maoz
Date:
At 18:05 +0200 on 17/11/98, Takashi Tokunaga wrote:


> 3. Now after that worked without any problem, so I now create SELECT
>    statement:
>
>   select * from test1 test2 where code_1 = test.code_2 \g
>   ERROR:  attribute 'code_2' not found
>
> nope that didn't work
>
>   select test1.name_1 from test1 test2 where test1.code_1 = test2.code_2 \g
>   ERROR:  attribute 'code_2' not found

You seem to have forgotten a comma...

SELECT *
FROM test1, test2   <---- Here
WHERE code1 = code2;

if you don't put a comma, "test2" will be used as an alias to "test1", not
as a table on its own.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma



Re: [SQL] How To Use JOIN?

From
"Oliver Elphick"
Date:
Takashi Tokunaga wrote:
  >Hello PGSQL users, I'm having trobule using join statement:

  >3. Now after that worked without any problem, so I now create SELECT
  >   statement:
  >
  >  select * from test1 test2 where code_1 = test.code_2 \g
  >  ERROR:  attribute 'code_2' not found
  >
  >nope that didn't work
  >
  >  select test1.name_1 from test1 test2 where test1.code_1 = test2.code_2 \g
  >  ERROR:  attribute 'code_2' not found
  >
  >Umm... still no change.

junk=>  select * from test1, test2 where code_1 = test2.code_2
junk-> ;
name_1|code_1|name_2|code_2
------+------+------+------
    10|     1|    11|     1
    11|     2|    11|     2
(2 rows)

junk=>  select test1.name_1 from test1, test2 where test1.code_1 = test2.code_2 ;
name_1
------
    10
    11
(2 rows)

You omitted the comma between the table names in the FROM clause.

--
Oliver Elphick                                Oliver.Elphick@lfix.co.uk
Isle of Wight                              http://www.lfix.co.uk/oliver
               PGP key from public servers; key ID 32B8FAA1
                 ========================================
     "For by grace are ye saved through faith; and that not
      of yourselves. It is the gift of God; not of works,
      lest any man should boast."    Ephesians 2:8,9