Professional Web Applications Themes

select next id in table? - MySQL

I have 2 tables. 1 is supposed to contain the id of the 'next' item to be viewed. from the other table, which has the data and also an id column. my problem ishow do I make a set of statements that UPDATEs the id (we'll call it pid) with the next row of the table, advancing it like a cursor, wrapping back to the beginning of the db? the DB is a version 4 database with MyISAM tables. I don't even know if it can be done outside of a tablescan+PHP. I have some broken code like this: $q1=mysql_query("SELECT ...

  1. #1

    Default select next id in table?

    I have 2 tables. 1 is supposed to contain the id of the 'next' item to be
    viewed. from the other table, which has the data and also an id column.

    my problem ishow do I make a set of statements that UPDATEs the id (we'll
    call it pid) with the next row of the table, advancing it like a cursor,
    wrapping back to the beginning of the db?
    the DB is a version 4 database with MyISAM tables.

    I don't even know if it can be done outside of a tablescan+PHP.
    I have some broken code like this:
    $q1=mysql_query("SELECT pid FROM idx");
    if ($row1=mysql_fetch_assoc($q1)) {
    $a=array();
    $q2=mysql_query("SELECT pid FROM pictures");
    while ($row2=mysql_fetch_assoc($q2)) {
    $a[]=$row2['pid'];
    }
    $i=array_search($row1['pid'], $a); //search for idx's pid in sea of
    table's pids
    $n=$a[$i];
    $i++; //next picture. sorry. tried random. doesn't work. get same pix
    every time.
    $i%=count($a);
    mysql_query("UPDATE idx SET pid=$a[$i]", $link2) or
    die("ERRUV2:".mysql_error());
    ....
    }

    Can this be done with queries?


    Jim Michaels Guest

  2. #2

    Default Re: select next id in table?

    Jim Michaels wrote:
    > I have 2 tables. 1 is supposed to contain the id of the 'next' item to be
    > viewed. from the other table, which has the data and also an id column.
    >
    > my problem ishow do I make a set of statements that UPDATEs the id (we'll
    > call it pid) with the next row of the table, advancing it like a cursor,
    > wrapping back to the beginning of the db?
    > the DB is a version 4 database with MyISAM tables.
    >
    > I don't even know if it can be done outside of a tablescan+PHP.
    > I have some broken code like this:
    > $q1=mysql_query("SELECT pid FROM idx");
    > if ($row1=mysql_fetch_assoc($q1)) {
    > $a=array();
    > $q2=mysql_query("SELECT pid FROM pictures");
    > while ($row2=mysql_fetch_assoc($q2)) {
    > $a[]=$row2['pid'];
    > }
    > $i=array_search($row1['pid'], $a); //search for idx's pid in sea of
    > table's pids
    > $n=$a[$i];
    > $i++; //next picture. sorry. tried random. doesn't work. get same pix
    > every time.
    > $i%=count($a);
    > mysql_query("UPDATE idx SET pid=$a[$i]", $link2) or
    > die("ERRUV2:".mysql_error());
    > ...
    > }
    >
    > Can this be done with queries?
    >
    >
    Assuming the column name is pid, the current record has been selected into
    $curpid and the rows are in ascending (although not necessarily consecutive) order:

    mysql_query("SELECT pid FROM pictures WHERE pid > $curpid LIMIT 1);


    --
    ==================
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    [email]jstucklexattglobal.net[/email]
    ==================
    Jerry Stuckle Guest

Similar Threads

  1. insert into...select using the same table
    By Don Vaillancourt in forum MySQL
    Replies: 2
    Last Post: March 22nd, 10:30 PM
  2. multi table select
    By Chuck Anderson in forum MySQL
    Replies: 3
    Last Post: February 10th, 06:39 AM
  3. How to select record on one table but not the other
    By Rustywater in forum Coldfusion Database Access
    Replies: 1
    Last Post: September 11th, 11:15 AM
  4. randomly select a table, better way?
    By Daniel Bray in forum PHP Development
    Replies: 3
    Last Post: September 2nd, 06:25 AM
  5. Select all the user table
    By hngo01 in forum Microsoft SQL / MS SQL Server
    Replies: 4
    Last Post: July 9th, 05:13 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139