Jump to content

please help with joins


tomasd

Recommended Posts

Hi, I'm having 3 tables;

 

SQL> describe event_staff;

Name                                      Null?    Type

----------------------------------------- -------- ----------------------------

STAFF_ID                                  NOT NULL NUMBER(38)

EVENT_ID                                  NOT NULL VARCHAR2(2)

 

SQL> describe event_staff_agy;

Name                                      Null?    Type

----------------------------------------- -------- ----------------------------

STAFF_AGY_ID                              NOT NULL NUMBER(38)

EVENT_ID                                  NOT NULL VARCHAR2(2)

 

SQL> describe event;

Name                                      Null?    Type

----------------------------------------- -------- ----------------------------

LOCATION                                  NOT NULL VARCHAR2(10)

ID                                        NOT NULL VARCHAR2(2)

CLIENT_ID                                NOT NULL NUMBER(38)

 

SQL> select * from event_staff;

 

  STAFF_ID EV

---------- --

        1 1

        2 1

 

SQL> select * from event_staff_agy;

 

STAFF_AGY_ID EV

------------ --

          1 1

          2 1

 

SQL> select * from event;

 

LOCATION  ID  CLIENT_ID

---------- -- ----------

Loc 1      1          1

Loc 2      2          1

 

 

I'm trying to join those tables using;

SQL> SELECT

        EVENT.ID,

        EVENT_STAFF.STAFF_ID,

        EVENT_STAFF_AGY.STAFF_AGY_ID

FROM(

(EVENT INNER JOIN EVENT_STAFF ON EVENT.ID = EVENT_STAFF.EVENT_ID)

INNER JOIN EVENT_STAFF_AGY ON EVENT.ID = EVENT_STAFF_AGY.EVENT_ID

);

  2    3    4    5    6    7    8

ID  STAFF_ID STAFF_AGY_ID

-- ---------- ------------

1          1            1

1          1            2

1          2            1

1          2            2

 

I guess this is not correct as the result I'm expecting is;

ID  STAFF_ID STAFF_AGY_ID

-- ---------- ------------

1          1           

1          2           

1                        1

1                        2

 

what would be the right way of joining this?

Any help is appreciated!

Link to comment
https://forums.phpfreaks.com/topic/101991-please-help-with-joins/
Share on other sites

To get the results you expect you would need a UNION

SELECT
        EVENT.ID,
        EVENT_STAFF.STAFF_ID,
        '' AS STAFF_AGY_ID
FROM
EVENT INNER JOIN EVENT_STAFF ON EVENT.ID = EVENT_STAFF.EVENT_ID
UNION
SELECT
        EVENT.ID,
        '' AS STAFF_ID,
        EVENT_STAFF_AGY.STAFF_AGY_ID
FROM
EVENT
INNER JOIN EVENT_STAFF_AGY ON EVENT.ID = EVENT_STAFF_AGY.EVENT_ID

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

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.