SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast bokz_42's Avatar
    Join Date
    Sep 2006
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP and Mysql Random

    Hi

    just wanted to know the solution for this problem. this has been bugging me for days!!

    not really sure if this is a php thing or mysql thing and sorry in advance if i posted this in the wrong channel.

    well here it goes...

    say i have 2 mysql queries

    $query_1 = "SELECT * FROM `table` ORDER BY RAND()";

    $query_2 = "SELECT * FROM `table` ORDER BY RAND()";

    when i fetch the result of $query_1 and $query_2, some of matches with the other. what i need is to make all of the returned values of $query_1 different from the returned values of $query_2.

    for example

    $query_1 returns

    2,5,4,3,7

    $query_2 should return

    1,6,10,8,9

    thanks guys~ more power!

  2. #2
    SitePoint Enthusiast bokz_42's Avatar
    Join Date
    Sep 2006
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP and Mysql Random

    Hi

    just wanted to know the solution for this problem. this has been bugging me for days!!

    not really sure if this is a php thing or mysql thing and sorry in advance if i posted this in the wrong channel.

    well here it goes...

    say i have 2 mysql queries

    $query_1 = "SELECT * FROM `table` ORDER BY RAND()";

    $query_2 = "SELECT * FROM `table` ORDER BY RAND()";

    when i fetch the result of $query_1 and $query_2, some of matches with the other. what i need is to make all of the returned values of $query_1 different from the returned values of $query_2.

    for example

    $query_1 returns

    2,5,4,3,7

    $query_2 should return

    1,6,10,8,9

    thanks guys~ more power!

  3. #3
    SitePoint Addict
    Join Date
    Jul 2008
    Location
    sudo rm -rf /
    Posts
    386
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think this will be possible without checking the rows.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,595
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Since both queries will return all the results just in different orders there will be no entries in one not in the other.

    If you want to split the entries into two lists with half randomly selected for each list then retrieve them using one select and then use PHP to add alternate entries to two different arrays.
    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="^$">

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,014
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by bokz_42 View Post
    not really sure if this is a php thing or mysql thing and sorry in advance if i posted this in the wrong channel.
    i can understand your indecision, but posting the same question in both places was definitely the wrong move

    i've merged the two threads with their different replies together

    i like felgall's answer, it's not only simple but most efficient as well
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •