Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XCdOB-00086d-VW for pgsql-sql@arkaria.postgresql.org; Wed, 30 Jul 2014 23:34:32 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XCdOB-0004Kv-7W for pgsql-sql@arkaria.postgresql.org; Wed, 30 Jul 2014 23:34:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XCdOA-0004Kn-Bn for pgsql-sql@postgresql.org; Wed, 30 Jul 2014 23:34:30 +0000 Received: from mbx.knossos.net.nz ([202.160.48.10]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XCdO3-0006z0-Us for pgsql-sql@postgresql.org; Wed, 30 Jul 2014 23:34:28 +0000 Received: from [10.1.1.3] (121-99-185-67.bng1.nct.orcon.net.nz [121.99.185.67]) (authenticated bits=0) by mbx.knossos.net.nz (8.14.4/8.14.4) with ESMTP id s6UNY8NT009068 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NOT); Thu, 31 Jul 2014 11:34:14 +1200 Message-ID: <53D980F0.8020603@archidevsys.co.nz> Date: Thu, 31 Jul 2014 11:34:08 +1200 From: Gavin Flower Organization: ArchiDevSys User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: CrashBandi , pgsql-sql@postgresql.org Subject: Re: Reg: Sql Join References: In-Reply-To: Content-Type: multipart/alternative; boundary="------------060607010502010307020509" X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org This is a multi-part message in MIME format. --------------060607010502010307020509 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 31/07/14 10:08, CrashBandi wrote: > table A > name col1 col2 col3 col4 > apple 100 11111 1 APL > orange 200 22222 3 ORG > carrot 300 33333 3 CRT > > > > table B > custom_name value obj_type obj_id > apple a FR 100 > orange o FR 200 > carrot c VG 300 > apple d FR 11111 > orange e VG 22222 > carrot f UC 33333 > apple h VG 1 > orange o FR 3 > carrot c VG 3 > Can't actually do joins the way you want but consider the following... DROP TABLE IF EXISTS table_a; DROP TABLE IF EXISTS table_b; CREATE TABLE table_a ( id SERIAL PRIMARY KEY, name text, col1 int, col2 int, col3 int, col4 text ); CREATE TABLE table_b ( id SERIAL PRIMARY KEY, custom_name text, value text, obj_type text, obj_id int ); INSERT INTO table_a (name, col1, col2, col3, col4) VALUES ('apple', 100, 11111, 1, 'APL'), ('orange', 200, 22222, 3, 'ORG'), ('carrot', 300, 33333, 3, 'CRT') /**/;/**/ INSERT INTO table_b (custom_name, value, obj_type, obj_id) VALUES ('apple', 'a', 'FR', 100), ('orange', 'o', 'FR', 200), ('carrot', 'c', 'VG', 300), ('apple', 'd', 'FR', 11111), ('orange', 'e', 'VG', 22222), ('carrot', 'f', 'UC', 33333), ('apple', 'h', 'VG', 1), ('orange', 'o', 'FR', 3), ('carrot', 'c', 'VG', 3) /**/;/**/ SELECT * FROM table_a a, table_b b WHERE ( obj_type ='FR' AND obj_id = col1 ) OR ( obj_type ='VG' AND obj_id = col2 ) OR ( obj_type ='UC' AND obj_id = col2 ); SELECT * FROM table_a a, table_b b WHERE obj_type ='FR' AND obj_id = col1 UNION SELECT * FROM table_a a, table_b b WHERE obj_type ='VG' AND obj_id = col2 UNION SELECT * FROM table_a a, table_b b WHERE obj_type ='UC' AND obj_id = col2 /**/;/**/ Cheers, Gavin --------------060607010502010307020509 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
On 31/07/14 10:08, CrashBandi wrote:
table A
name col1 col2 col3 col4
apple 100 11111 1 APL
orange 200 22222 3 ORG
carrot 300 33333 3 CRT


table B
custom_name value obj_type obj_id
apple a FR 100
orange o FR 200
carrot c VG 300
apple d FR 11111
orange e VG 22222
carrot f UC 33333
apple h VG 1
orange o FR 3
carrot c VG 3

Can't actually do joins the way you want but consider the following...

DROP TABLE IF EXISTS table_a;
DROP TABLE IF EXISTS table_b;


CREATE TABLE table_a
(
    id      SERIAL PRIMARY KEY,
    name    text,
    col1    int,
    col2    int,
    col3    int,
    col4    text   
);


CREATE TABLE table_b
(
    id          SERIAL PRIMARY KEY,
    custom_name text,
    value       text,
    obj_type    text,
    obj_id      int
);


INSERT INTO table_a
    (name, col1, col2, col3, col4)
VALUES
    ('apple', 100, 11111, 1, 'APL'),
    ('orange', 200, 22222, 3, 'ORG'),
    ('carrot', 300, 33333, 3, 'CRT')
/**/;/**/


INSERT INTO table_b
    (custom_name, value, obj_type, obj_id)
VALUES
    ('apple', 'a', 'FR', 100),
    ('orange', 'o', 'FR', 200),
    ('carrot', 'c', 'VG', 300),
    ('apple', 'd', 'FR', 11111),
    ('orange', 'e', 'VG', 22222),
    ('carrot', 'f', 'UC', 33333),
    ('apple', 'h', 'VG', 1),
    ('orange', 'o', 'FR', 3),
    ('carrot', 'c', 'VG', 3)
/**/;/**/


SELECT
    *
FROM
    table_a a,
    table_b b
WHERE
    (
        obj_type ='FR'
        AND
        obj_id = col1
    )
    OR   
    (
        obj_type ='VG'
        AND
        obj_id = col2
    )
    OR
    (
        obj_type ='UC'
        AND
        obj_id = col2
    );


SELECT
    *
FROM
    table_a a,
    table_b b
WHERE
        obj_type ='FR'
    AND obj_id = col1
UNION
SELECT
    *
FROM
    table_a a,
    table_b b
WHERE
        obj_type ='VG'
    AND obj_id = col2
UNION
SELECT
    *
FROM
    table_a a,
    table_b b
WHERE
        obj_type ='UC'
    AND obj_id = col2
/**/;/**/


Cheers,
Gavin
--------------060607010502010307020509--