SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Msql select

  1. #1
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    scarborough
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Msql select

    I am making a forum. I am trying to select about twenty records from a table... basically building a page which lists the various posts and their titles.

    I want to do something like this ...

    SELECT FROM listposts WHERE recordnumber=126 or recordnumber=437 or recordnumber=174 or recordnumber=4321 or recordnumber=46 or recordnumber=39

    So I want to select all records which match a list of recordnumbers. Is there a special way I can do this?

    I do not have great experience with msql so sorry or any ignorance

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    scarborough
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I basically want to select all records which match an array eg

    $test=Array(17,145,14);


    $result = mysql_query("SELECT postnumber,timedate FROM list WHERE postnumber='$test'");

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Sure.
    Code:
    SELECT this FROM that WHERE other IN (1, 2, 3, 4)
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,026
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hairybobby View Post
    I basically want to select all records which match an array eg

    $test=Array(17,145,14);


    $result = mysql_query("SELECT postnumber,timedate FROM list WHERE postnumber='$test'");
    Quote Originally Posted by AnthonySterling View Post
    Sure.
    Code:
    SELECT this FROM that WHERE other IN (1, 2, 3, 4)
    He'll need to use implode

    Code php:
    $s = $pdo->prepare("
      SELECT postnumber, timedate 
      FROM list 
      WHERE postnumber IN (".implode(',',$test).")
    ");
     
    $s->execute();

    PDO is prefered to the mysql (or mssql) function libraries.

    If you got that list of record id's from another query it's faster to just nest the queries like this

    Code sql:
    SELECT postnumber, timedate
    FROM list
    WHERE postnumber IN (
      SELECT postnumber
      FROM ??....
    )

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    scarborough
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everyone

    Michael : The execute and prepare were not needed as the implode worked well on its own eg

    $result = mysql_query("SELECT postnumber,timedate,dream FROM list WHERE postnumber IN (".implode(',',$test).")");


    You guessed correctly that the array comes from another select and I would need to nest selects


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
  •