SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Opposite of UNION

    The 1st query calls row(1), row(4) and the 2nd query calls row(2) and row(4).

    The 1st query UNION the 2nd query calls row(1), row(2) and row(4).

    Then, How can I call common row only?

    The 1st query OPPOSITE of UNION 2nd query calls row(4) only.

    Is there any operator or function which is OPPOSITE of UNION?

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Normally you would do that with an INTERSECT (which is the opposite of union), but as far as I know MySQL doesn't support INTERSECT.

    So what you could do is something like:

    Code:
    SELECT
      field
    FROM
       my_table
    WHERE
       field IN (<second select query>)
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can do several unions so lon as you ensure they return the same number of values

    Code MySQL:
    select
    col1
    , col2
    , NULL
    from table 1 AS t1
    UNION ALL
    select
    col2
    , NULL
    , NULL
    from table AS t2
    UNION ALL
    select 
    NULL
    , col3
    , col4
    from table AS t3

    Thats not a full query but is meant to show how using NULL can let you have a query with differing numbers of columns in each union. ALso meant to show UNION ALL, which you can google for.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •