SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to Use IN operator

    I want to use IN operator in sql query please suggest the correct syntax. code snippest is just example.

    Problem is who to use $selectedusers[] array in Query using IN operator.


    $sql = "select userid from tblprojuser where projid = '$sprojid'";
    $result = mysqli_query($link, $sql);

    while ($row = mysqli_fetch_array($result))
    {$selectedusers[] = $row['userid'];}


    //selecte all application users excluding those in $selectedusers[] array
    $sql = "select id, username, status from tbluser where id NOT IN ($selectedusers)";

  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)
    PHP Code:
    $sql "select id, username, status from tbluser where id NOT IN (" implode(","$selectedusers) . ")"

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    If you don't need the result of your first query for anything else, you can do it with one query:
    Code MySQL:
    SELECT
        id
      , username
      , status
    FROM tbluser
    LEFT OUTER JOIN tblprojuser
    ON userid = id
    AND projid = '$sprojid'
    WHERE userid IS NULL


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
  •