1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Select from multiple tables

Discussion in 'Databases' started by php-lover, Jun 12, 2008.

  1. #1
    Hi guys,

    I have two tables (tbl1,tbl2)

    I was trying to run a single select query to select all the record from tbl1 and tbl2 but I got an empty result.

    Here's my query:

    SELECT a.*,b.* FROM tbl1 as a,tbl2 as b;

    I found that the problem is tbl1 has no record but tbl2 has a record inside.

    If tbl1 has no record, is there any way that I can still select all from tbl1 and tbl2 to get all the record from tbl2 instead of an empty result.

    Thanks for any help.

    Nau.
     
    php-lover, Jun 12, 2008 IP
  2. in2clearsky

    in2clearsky Peon

    Messages:
    121
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    No join? Try to use outer join. Or if these two tables have same structure, use union.
     
    in2clearsky, Jun 12, 2008 IP
  3. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    As mentioned, you can use JOIN. Another way is using a union of two queries

    select * from tbl1
    union
    select * from tbl2
    Code (markup):
    But this will work only if the two tables have the same columns. You can still use this even if the columns are different, but it's a bit tricky and not always efficient...
    Post some more info if you haven't figured it yet
     
    xlcho, Jun 13, 2008 IP
  4. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Thanks guys, I change my table structure and it works fine.

    Cheers
     
    php-lover, Jun 13, 2008 IP
  5. dreammen

    dreammen Guest

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You should change your codes.
    select * from tbl1
    union
    select * form tbl2



    :eek:
     
    dreammen, Jun 14, 2009 IP
  6. SuperTight

    SuperTight Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I found this at

    [​IMG]

    depending on what database platform you are using, the correct syntax probably looks something like:

    SELECT *
    FROM TableA AS a
    FULL JOIN TableB AS b ON a.TableKey = b.TableKey

    If for some reason you have no shared key value between the two tables you could do this:

    SELECT *
    FROM TableA AS a
    FULL JOIN TableB AS b ON 1 = 1 --or a=a or any comparison that will always guarantee a match

    If you want to join the two tables VERTICALLY rather than HORIZONTALLY (add rows to the data set, not columns), then you could use the UNION syntax suggested previously:

    SELECT * FROM TableA
    UNION ALL SELECT * FROM TableB
     
    SuperTight, Jun 21, 2009 IP
  7. livedating

    livedating Active Member

    Messages:
    161
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    83
  8. ram4nd

    ram4nd Active Member

    Messages:
    167
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    50
    #8
    I prefer this method over joins and unions:
    SELECT table1.column1, table2.column2 FROM table1, table2 WHERE table1.column1 = table2.column1;
     
    ram4nd, Jul 10, 2012 IP
  9. Neelk

    Neelk Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    we have two mathod..one is using "join" and second one is "union"...
     
    Neelk, Jul 12, 2012 IP
  10. sourabhsharma.obizm

    sourabhsharma.obizm Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    SELECT *
    FROM tbl1 t1
    LEFT JOIN tbl2 t2
    ON t1.col = t2.col
    UNION
    SELECT *
    FROM tbl1 t1
    RIGHT JOIN tbl2 t2
    ON t1.col>= t2.<col
     
    sourabhsharma.obizm, Aug 24, 2012 IP