[PHP] random order by id
Hey guys, i have a db and a table with id and questions now i want these questions to be listed in a random order, is there a way to format my SQL query or do i need some PHP work to? i can ofcourse select a random one, that's no problem, but how do i make sure that that one does not get chosen again. and that after it has printed all the records it stops any ideas? Jule -- |\/\__/\/| | Jule Slootbeek | | [EMAIL PROTECTED]| | http://blindtheory.cjb.net | | __ | |/\/ \/\| -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] random order by id
If you are using MySQL try looking at this page for the function rand(): http://www.mysql.com/doc/M/a/Mathematical_functions.html Here's a little overview of what it says: In MySQL Version 3.23, you can, however, do: SELECT * FROM table_name ORDER BY RAND() This is useful to get a random sample of a set SELECT * FROM table1,table2 WHERE a=b AND cd ORDER BY RAND() LIMIT 1000. Note that a RAND() in a WHERE clause will be re-evaluated every time the WHERE is executed. RAND() is not meant to be a perfect random generator, but instead a fast way to generate ad hoc random numbers that will be portable between platforms for the same MySQL version. Cheers! Rick Nobody will ever win the Battle of the Sexes. There's just too much fraternizing with the enemy. - Henry Kissinger From: Jule [EMAIL PROTECTED] Date: Sat, 18 May 2002 17:41:59 -0400 To: [EMAIL PROTECTED] Subject: [PHP] random order by id Hey guys, i have a db and a table with id and questions now i want these questions to be listed in a random order, is there a way to format my SQL query or do i need some PHP work to? i can ofcourse select a random one, that's no problem, but how do i make sure that that one does not get chosen again. and that after it has printed all the records it stops any ideas? Jule -- |\/\__/\/| | Jule Slootbeek | | [EMAIL PROTECTED] | | http://blindtheory.cjb.net | | __ | |/\/ \/\| -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] random order by id
i have a db and a table with id and questions now i want these questions to be listed in a random order, is there a way to format my SQL query or do i need some PHP work to? It's best achieved in sql: SELECT ID, Question FROM Questions WHERE Some = Condition ORDER BY RAND() LIMIT 10 Will get you up to ten randomised questions based on your 'where' condition. CYA, Dave -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] random order
Well, all I can say is that SELECT * FROM table ORDER BY id, RAND(); works just fine. It may not be standard SQL, but MySQL handles it just fine. If you need to make it a table field, then you can do it this way: SELECT *, RAND() AS r FROM table ORDER BY id, r; I would hope that query would work in other database programs, too. ---John Holmes... -Original Message- From: Neil Zanella [mailto:[EMAIL PROTECTED]] Sent: Sunday, May 12, 2002 9:13 PM To: John Holmes Cc: 'Jule'; PHP General Mailing List; MySQL Mailing List Subject: RE: [PHP] random order Only table field names are allowed after the ORDER BY clause in standard SQL. The answer to your question is that SQL is not designed to do that kind of thing. Rather, I would suggest computing a random permutation of the numbers 1, 2, 3, and 4 in the PHP application prior to constructing your SQL statement and passing it to MySQL. Furthermore, if your application does not support the generation of random numbers (although I am pretty sure it does), you could always retrieve random numbers from the MySQL built in function RAND() by issuing SELECT RAND() statements (although this approach is more inefficient as it consumes bandwidth). Neil On Sun, 12 May 2002, John Holmes wrote: I don't know if this will work or not, but try SELECT * FROM table ORDER BY id, RAND(); That should keep all of the questions and answers together, but give a random order for the answers.. ?? Like I said, not exactly sure on it though. Best option, like someone else said, is to split this up into two tables. ---John Holmes... -Original Message- From: Jule [mailto:[EMAIL PROTECTED]] Sent: Sunday, May 12, 2002 1:28 PM To: [EMAIL PROTECTED]; [EMAIL PROTECTED]; [EMAIL PROTECTED] Subject: Re: [PHP] random order hmm it got a little more complicated now, i have a table with: id title question answer1 answer2 answer3 answer4. how can i use all of those in a php page but only randomize the answers? so i get title title question question answer3 answer4 answer2 answer1 answer4 answer3 answer1 answer2 any ideas? thanks, Jule On Sunday 12 May 2002 17:30, you wrote: Add on a LIMIT to restrict it to return a certain number of rows. If you had a table of say, 100 rows, and you wanted four random ones out of the 100, then you'd use this: SELECT * FROM table ORDER BY RAND() LIMIT 4; ---John Holmes... -Original Message- From: Jule [mailto:[EMAIL PROTECTED]] Sent: Sunday, May 12, 2002 11:22 AM To: [EMAIL PROTECTED] Subject: Re: [PHP] random order Thanks John, I thought that that command only selected a random row.. must've read wrong. thanks. Jule On Sunday 12 May 2002 17:21, you wrote: Nothing to do with PHP... SELECT * FROM table ORDER BY RAND(); ---John Holmes... -Original Message- From: Jule [mailto:[EMAIL PROTECTED]] Sent: Sunday, May 12, 2002 9:49 AM To: [EMAIL PROTECTED] Subject: [PHP] random order Hey guys and gals, I have a problem: I have a mysql databse with 4 rows, and each row (row1, row2, row3, and row4) contains a sentence. now i want these sentences to appear in random order: instance: 1 2etc. row2 row4 row4 row2 row1 row1 row3 row3 how do i go about that?? any ideas apreciated. thanks Jule -- |\/\__/\/| | Jule Slootbeek | | [EMAIL PROTECTED] | | http://blindtheory.cjb.net | | __ | |/\/\/\| -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- |\/\__/\/| | Jule Slootbeek | | [EMAIL PROTECTED] | | http://blindtheory.cjb.net | | __ | |/\/\/\| -- |\/\__/\/| | Jule Slootbeek | | [EMAIL PROTECTED] | | http://blindtheory.cjb.net | | __ | |/\/\/\| - Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail [EMAIL PROTECTED] To unsubscribe, e-mail mysql-unsubscribe- [EMAIL PROTECTED] Trouble
[PHP] random order
Hey guys and gals, I have a problem: I have a mysql databse with 4 rows, and each row (row1, row2, row3, and row4) contains a sentence. now i want these sentences to appear in random order: instance: 1 2etc. row2 row4 row4 row2 row1 row1 row3 row3 how do i go about that?? any ideas apreciated. thanks Jule -- |\/\__/\/| | Jule Slootbeek | | [EMAIL PROTECTED]| | http://blindtheory.cjb.net | | __ | |/\/ \/\| -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] random order
Nothing to do with PHP... SELECT * FROM table ORDER BY RAND(); ---John Holmes... -Original Message- From: Jule [mailto:[EMAIL PROTECTED]] Sent: Sunday, May 12, 2002 9:49 AM To: [EMAIL PROTECTED] Subject: [PHP] random order Hey guys and gals, I have a problem: I have a mysql databse with 4 rows, and each row (row1, row2, row3, and row4) contains a sentence. now i want these sentences to appear in random order: instance: 1 2etc. row2 row4 row4 row2 row1 row1 row3 row3 how do i go about that?? any ideas apreciated. thanks Jule -- |\/\__/\/| | Jule Slootbeek | | [EMAIL PROTECTED] | | http://blindtheory.cjb.net | | __ | |/\/\/\| -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] random order
hmm it got a little more complicated now, i have a table with: id title question answer1 answer2 answer3 answer4. how can i use all of those in a php page but only randomize the answers? so i get title title questionquestion answer3 answer4 answer2 answer1 answer4 answer3 answer1 answer2 any ideas? thanks, Jule On Sunday 12 May 2002 17:30, you wrote: Add on a LIMIT to restrict it to return a certain number of rows. If you had a table of say, 100 rows, and you wanted four random ones out of the 100, then you'd use this: SELECT * FROM table ORDER BY RAND() LIMIT 4; ---John Holmes... -Original Message- From: Jule [mailto:[EMAIL PROTECTED]] Sent: Sunday, May 12, 2002 11:22 AM To: [EMAIL PROTECTED] Subject: Re: [PHP] random order Thanks John, I thought that that command only selected a random row.. must've read wrong. thanks. Jule On Sunday 12 May 2002 17:21, you wrote: Nothing to do with PHP... SELECT * FROM table ORDER BY RAND(); ---John Holmes... -Original Message- From: Jule [mailto:[EMAIL PROTECTED]] Sent: Sunday, May 12, 2002 9:49 AM To: [EMAIL PROTECTED] Subject: [PHP] random order Hey guys and gals, I have a problem: I have a mysql databse with 4 rows, and each row (row1, row2, row3, and row4) contains a sentence. now i want these sentences to appear in random order: instance: 1 2etc. row2 row4 row4 row2 row1 row1 row3 row3 how do i go about that?? any ideas apreciated. thanks Jule -- |\/\__/\/| | Jule Slootbeek | | [EMAIL PROTECTED]| | http://blindtheory.cjb.net | | __ | |/\/ \/\| -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- |\/\__/\/| | Jule Slootbeek | | [EMAIL PROTECTED]| | http://blindtheory.cjb.net | | __ | |/\/ \/\| -- |\/\__/\/| | Jule Slootbeek | | [EMAIL PROTECTED]| | http://blindtheory.cjb.net | | __ | |/\/ \/\| -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] random order
hmm it got a little more complicated now, i have a table with: id title question answer1 answer2 answer3 answer4. how can i use all of those in a php page but only randomize the answers? You have multiple questions with the same answers? Then split your table into two tables and put all your answers in one and your questions in the other. Then include another table that contains question and answer id equivalents. CYA, Dave -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] random order
I have a mysql databse with 4 rows, and each row (row1, row2, row3, and row4) contains a sentence. now i want these sentences to appear in random order: I've always found mysql's RAND() function to be of use. Specifically: SELECT * FROM MyTable WHERE Some='Condition' ORDER BY RAND() LIMIT 4 CYA, Dave -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] random order
I don't know if this will work or not, but try SELECT * FROM table ORDER BY id, RAND(); That should keep all of the questions and answers together, but give a random order for the answers.. ?? Like I said, not exactly sure on it though. Best option, like someone else said, is to split this up into two tables. ---John Holmes... -Original Message- From: Jule [mailto:[EMAIL PROTECTED]] Sent: Sunday, May 12, 2002 1:28 PM To: [EMAIL PROTECTED]; [EMAIL PROTECTED]; [EMAIL PROTECTED] Subject: Re: [PHP] random order hmm it got a little more complicated now, i have a table with: id title question answer1 answer2 answer3 answer4. how can i use all of those in a php page but only randomize the answers? so i get title title question question answer3 answer4 answer2 answer1 answer4 answer3 answer1 answer2 any ideas? thanks, Jule On Sunday 12 May 2002 17:30, you wrote: Add on a LIMIT to restrict it to return a certain number of rows. If you had a table of say, 100 rows, and you wanted four random ones out of the 100, then you'd use this: SELECT * FROM table ORDER BY RAND() LIMIT 4; ---John Holmes... -Original Message- From: Jule [mailto:[EMAIL PROTECTED]] Sent: Sunday, May 12, 2002 11:22 AM To: [EMAIL PROTECTED] Subject: Re: [PHP] random order Thanks John, I thought that that command only selected a random row.. must've read wrong. thanks. Jule On Sunday 12 May 2002 17:21, you wrote: Nothing to do with PHP... SELECT * FROM table ORDER BY RAND(); ---John Holmes... -Original Message- From: Jule [mailto:[EMAIL PROTECTED]] Sent: Sunday, May 12, 2002 9:49 AM To: [EMAIL PROTECTED] Subject: [PHP] random order Hey guys and gals, I have a problem: I have a mysql databse with 4 rows, and each row (row1, row2, row3, and row4) contains a sentence. now i want these sentences to appear in random order: instance: 1 2etc. row2 row4 row4 row2 row1 row1 row3 row3 how do i go about that?? any ideas apreciated. thanks Jule -- |\/\__/\/| | Jule Slootbeek | | [EMAIL PROTECTED] | | http://blindtheory.cjb.net | | __ | |/\/\/\| -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- |\/\__/\/| | Jule Slootbeek | | [EMAIL PROTECTED] | | http://blindtheory.cjb.net | | __ | |/\/\/\| -- |\/\__/\/| | Jule Slootbeek | | [EMAIL PROTECTED] | | http://blindtheory.cjb.net | | __ | |/\/\/\| -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php