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.

Is it possible to have a multiple select in one query?

Discussion in 'Databases' started by 123GoToAndPlay, Mar 18, 2006.

  1. #1
    Hi

    Is it possible to have a multiple select in one query like
    
    SELECT * FROM images WHERE id = 8, 10, 4
    
    Code (markup):
    this ofcourse doesn't work
     
    123GoToAndPlay, Mar 18, 2006 IP
  2. Gaffer Sports

    Gaffer Sports Guest

    Messages:
    144
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    SELECT * FROM images WHERE id = 8 OR id = 10 OR id = 4
    
    Code (markup):
    This should work for you.

    Steve
     
    Gaffer Sports, Mar 18, 2006 IP
  3. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    @gaffersports,

    well I found this one
    
    SELECT * FROM images WHERE id IN(8,10,4)
    
    Code (markup):
    Never used IN before so this will be the first time ;)

    Now I am stuck with the php part
    
    $sql = "SELECT * FROM images WHERE id IN (";
    	foreach($ids_today as $id_today )
    	{
    	   $sql .= ",$id_today";
    	  } 
    	  $sql .= ")";
    
    Code (markup):
    gives me
    
    SELECT * FROM images WHERE id IN(,8,10,4)
    
    Code (markup):
    need to get rid of the ,

    certainly going to try your suggestions as well.
     
    123GoToAndPlay, Mar 18, 2006 IP
  4. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ok with implode I have it up and running
    
    $tmp_ids = array();
    	 $sql = "SELECT * FROM images WHERE id IN (";
    	foreach($ids_today as $id_today )
    	{
    	   $tmps_ids[] = $id_today;
    	  } 
    	  $sql .= implode(",", $tmps_ids);
    	  $sql .= ")";
    
    	  echo $sql;	
    
    Code (markup):
    Not very elegant but that will be next I guess.
     
    123GoToAndPlay, Mar 18, 2006 IP
  5. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    oh rewritten it to
    
    $tmp_ids = array();
        foreach($ids_today as $id_today )
        {
           $tmps_ids[] = $id_today;
      }
         
          $sql = "SELECT * FROM images WHERE id IN (".implode(",", $tmps_ids).")";
    
    Code (markup):
     
    123GoToAndPlay, Mar 18, 2006 IP
  6. onlywin

    onlywin Greenhorn

    Messages:
    97
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #6
    yes, I paste your version and a version that works:

    your version: SELECT * FROM images WHERE id = 8, 10, 4

    correct: SELECT * FROM images WHERE id = 8 OR id = 10 OR id= 4
     
    onlywin, May 21, 2009 IP
  7. luizeba

    luizeba Active Member

    Messages:
    265
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #7
    Are you sure it works, onlywin?
     
    luizeba, May 24, 2009 IP
  8. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #8
    This is quite an old thread. Didn't you notice that the poster already posted the solution?
     
    mwasif, May 25, 2009 IP