PHP/mySQL: Combine two SELECT queries

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Boefje

    #1

    PHP/mySQL: Combine two SELECT queries

    Hi,

    How to get all the id's from the first query, which are not in the
    result set of the second query?


    All id's which are in



    SELECT spelerid
    FROM speler_team
    WHERE ! ( einddatum IS NULL )


    but NOT in


    SELECT spelerid
    FROM speler_team
    WHERE ( einddatum IS NULL )



    Example:
    result set query 1 is: 1,2,3,5,6,8,9
    result set query 2 is: 2,5,6,10

    I want to have 1,3,8,9



    Is this possible with one query? If yes, how?

    GB

    PS: Sorry for the cross groups posting. I am in a hurry


  • Erwin Moller

    #2
    Re: PHP/mySQL: Combine two SELECT queries

    Hi,

    have a look at UNION and EXCEPT

    notoriously slow, but works.

    Regards,
    Erwin Moller

    Comment

    • Mark Kuiphuis

      #3
      Re: PHP/mySQL: Combine two SELECT queries

      Loop through the first array and then call the in_array function with
      the spelerid as the first parameter and the second array as the second
      parameter...Mak e sure the resultSet has been put into an array first.

      <?php
      for($i = 0; $i < count($array1); $i++) {
      if (!in_array($arr ay1[$i], $array2)) {
      //This spelerid cannot be found in the 2nd array
      }
      }
      ?>

      Mark


      Boefje wrote:
      [color=blue]
      > Hi,
      >
      > How to get all the id's from the first query, which are not in the
      > result set of the second query?
      >
      >
      > All id's which are in
      >
      >
      >
      > SELECT spelerid
      > FROM speler_team
      > WHERE ! ( einddatum IS NULL )
      >
      >
      > but NOT in
      >
      >
      > SELECT spelerid
      > FROM speler_team
      > WHERE ( einddatum IS NULL )
      >
      >
      >
      > Example:
      > result set query 1 is: 1,2,3,5,6,8,9
      > result set query 2 is: 2,5,6,10
      >
      > I want to have 1,3,8,9
      >
      >
      >
      > Is this possible with one query? If yes, how?
      >
      > GB
      >
      > PS: Sorry for the cross groups posting. I am in a hurry
      >
      >[/color]

      Comment

      Working...