SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mysql query to pick up certain rows froma table

    Hello,

    I have a table called priest and it has only two fields - priestid and priestname.

    I want to pick up the rows where priestid=1, 5, 9, 13, 17,..............till the end of the table

    That means I want to pick up every 5th row (difference=4).

    In know the statement :

    select * from priest where priestid in (1,5,9)

    But I want mysql to pick up on its own till the end of the table......I am stuck with this.

    The following has not worked either:
    $r=1;
    $e=1;
    while ($e<51)
    {
    $result = mysql_query("SELECT * FROM priest where priestid limit $r,1");
    $r=$r+4;
    }

    Please help !!!!

  2. #2
    SitePoint Guru
    Join Date
    Jan 2004
    Location
    Uppsala, sverige
    Posts
    698
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Code:
    where mod(priestid,4) = 1

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,869
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Of course with mysql_query now long dead and about to be removed from PHP you really ought to be using mysqli_query instead.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot... That worked.

    One more query.......if I leave the IDs and want to show the priest name of the first row, then priestname form the 5th row, then 9th and so on (irrespective of the IDs), how can it be done??

    I have tried this with limit and offset but it did not work...

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,340
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Code:
    SELECT priestname
      FROM priest AS t
     WHERE MOD( ( SELECT COUNT(*)
                    FROM priest
                   WHERE priestid < t.priestid ),4) = 0
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Tags for this Thread

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
  •