SQL - selecting where id=list

Discussion in 'MySQL' started by pedrotuga, May 25, 2006.

  1. #1
    Dont really know how to do this...

    SELECT field
    FROM table
    WHERE id=1 or id=4 or id=6 ...


    i have the ids in a php array... now i need to know wich sintax to use in order to set up a loop to build my query string.

    thanks in advance
     
    pedrotuga, May 25, 2006 IP
  2. 2ndPlatform

    2ndPlatform Peon

    Messages:
    138
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hmm - what exactly are you trying to do? What is "list" - is that a value in another table? I should be able to help but I'm not quite sure what you're after...
     
    2ndPlatform, May 25, 2006 IP
  3. pedrotuga

    pedrotuga Peon

    Messages:
    162
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    look at the example...
    i am using OR OR OR OR OR OR....

    isnt there any other way to do that?
     
    pedrotuga, May 26, 2006 IP
  4. 2ndPlatform

    2ndPlatform Peon

    Messages:
    138
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You need to wrap it in a while loop... here's the basic syntax:

    myArray = array(1,2,3,4);
    
    while (x<5) {
    	 SELECT field FROM table WHERE id=myArray[x];
    }
    Code (markup):
    Put whatever code you want to duplicate within the {} of the while loop.
     
    2ndPlatform, May 26, 2006 IP
  5. pedrotuga

    pedrotuga Peon

    Messages:
    162
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    mmmmm.... thats like plsql...

    i am not so aware of what is possible to do with mysql.

    does mysql allows function definitions?that could make thinks easier...

    i also need a way to parse the php array to a mysql array....
    i guess i can build the array sql string... or is there any more efficient way?
     
    pedrotuga, May 26, 2006 IP
  6. Chemo

    Chemo Peon

    Messages:
    146
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    
    $idArray = array('1', '2', '3');
    $sql = 'SELECT field FROM table WHERE id IN (' . implode(',', $idArray) . ')';
    $query = mysql_query($sql);
    if ( mysql_num_rows($query) > 0 ){
      //retrieve the rows
    }
    
    PHP:
     
    Chemo, Jun 10, 2006 IP
    sketch and Crazy_Rob like this.
  7. pedrotuga

    pedrotuga Peon

    Messages:
    162
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    thx chemo... i already have it working... i used the implode function, i've been told by somebody outrside here. But thanks anyway for the repply ;) preciated
     
    pedrotuga, Jun 10, 2006 IP