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.

Anyone help me with UNION?

Discussion in 'MySQL' started by D_C, Apr 10, 2007.

  1. #1
    What exactly does this do? What commands does this send to the sql server and what should/can follow the command. I think there is a UNION ALL command too.

    Sorry, I'm new to SQL and am learning. I do not know much more than SELECT and other things similar to that.

    Thanks.
    SEMrush
     
    D_C, Apr 10, 2007 IP
    SEMrush
  2. druidelder

    druidelder Peon

    Messages:
    286
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Union is used to pull two matching datatypes together in one column. Let's say, for example, you have a forum. In the database for that forum you have two tables tblAdminUser and tblUser. You want to do a mass email to all users, so you can use union to pull out the email adress from both tables. Union pulls out distinct values. Union All shows the repeated values (if any).

    example:

    select email from tblAdminUsers
    UNION
    select email from tblUsers

    This would give you one list of all distinct email addresses in both tables.
     
    druidelder, Apr 10, 2007 IP
    D_C likes this.
  3. D_C

    D_C Well-Known Member

    Messages:
    1,107
    Likes Received:
    21
    Best Answers:
    1
    Trophy Points:
    160
    #3
    Thanks for the information!
     
    D_C, Apr 10, 2007 IP
  4. John@PP

    John@PP Peon

    Messages:
    33
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You could use a JOIN query with something link

    SELECT DISTINCT(users.email) FROM admins JOIN users where admins.email = users.email GROUP BY users.email
     
    John@PP, Apr 10, 2007 IP
  5. veridicus

    veridicus Peon

    Messages:
    52
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    No, your JOIN limits to only return where the emails match. UNION returns all from both tables.
     
    veridicus, Apr 10, 2007 IP
  6. druidelder

    druidelder Peon

    Messages:
    286
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Union returns distinct values. Union all returns all.

    Union create one column from two tables. JOIN creates a table that contains all columns from both tables.

    Be careful with JOINS in MySQL. Sometimes the JOIN happens before the where clause and sometimes after.
     
    druidelder, Apr 10, 2007 IP