views:

121

answers:

3

I've got three tables

AUTHOR_TABLE
-------------
AUTHOR_ID (PK)   AUTHOR_NAME   
1                me
2                you 

ARTICLE_AUTHOR_TABLE
-------------
AUTHOR_ID  ARTICLE_ID  
1          100
2          101


EVENT_AUTHOR_TABLE
------------------------------------------
AUTHOR_ID   EVENT_ID  
1           200
1           201

All I want is either

RESULTS
-----------------------------------------
AUTHOR_ID AUTHOR_NAME SOURCE_TABLE ID
1         me          article      100
2         you         article      101
1         me          event        200
1         me          event        201 

/* where SOURCE_TABLE would be either "EVENT" or "ARTICLE" */

EDIT I don't really want this

RESULTS
-----------------------------------------
AUTHOR_ID AUTHOR_NAME EVENT_ID ARTICLE_ID
1         me          NULL         100
2         you         NULL         101
1         me          200         NULL
1         me          201         NULL

Any pointers appreciated.

THanks

+1  A: 
SELECT A.AUTHOR_ID, A.AUTHOR_NAME, EA.EVENT_ID, AA.ARTICLE_ID
FROM AUTHOR_TABLE AS A
    LEFT JOIN ARTICLE_AUTHOR_TABLE AS AA ON AA.AUTHOR_ID = A.AUTHOR_ID
    LEFT JOIN EVENT_AUTHOR_TALBE AS EA ON EA.AUTHOR_ID = A.AUTHOR_ID
Koistya Navin
This doesn't output what he's asking for... he wants to show both articles and events in the same resultset, not just articles.
Seb
Now it's better :) But there's a way to fetch the first desired result; look at my answer.
Seb
+3  A: 
SELECT
  at.author_id,
  at.author_name,
  'article' AS source_table,
  aat.id
FROM
  author_table at
  JOIN article_author_table aat
    ON at.author_id = aat.author_id

UNION ALL

SELECT
  at.author_id,
  at.author_name,
  'event' AS source_table,
  eat.id
FROM
  author_table at
  JOIN event_author_table eat
    ON at.author_id = eat.author_id
Seb
I actually want to join about 10 tables together like this, is that a bad idea?
DrG
It depends on the amount of rows... but yes, it's usually a bad idea to have so many UNIONs. It mainly reflects poorly designed DBs, so if you can it would be a good a idea to restructure them. If not, again, it will depend on how many rows you're expecting to handle.
Seb
OK Thanks, yes it is poor database design (from yours truly) something I did about 10 years ago, and am just writing an ad-hoc report, so I'll just have to live with it for now :D
DrG
LOL well, I guess you're not alone cursing old code, specially written by ourselves ;)
Seb
A: 

Since this is homework, and i am guessing you are using MySQL, read this documentation on the UNION syntax. What you are doing is basically doing two (or more) queries from similar databases and getting the results in one query.

Be advised that the resulting columns need to be the same.

You can also create variables to show what table you are calling from.

SELECT AUTHOR_ID, AUTHOR_NAME, `AUTHOR_TABLE` AS SOURCE_TABLE, ID FROM AUTHOR_TABLE

You do this will all the union's but change the table name.

Ólafur Waage
Btw since this is homework i have decided not to do the full answer, like Seb did.
Ólafur Waage
BTW it's not homework, I just simplified it to look like homework :D
DrG
When I answered, it was not tagged as homework :) I hate when people tag other's questions as homework just because they seem simple.
Seb