Jump to content


Photo

stop repeating question


  • Please log in to reply
3 replies to this topic

#1 masgas

masgas
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 18 February 2006 - 02:57 PM

Hi I've got this code for selecting a number of questions from a db to make a test. It's random and sometimes it selects the same questions and they appear twice in the tests... how could I stop this from happening?

$sql = "SELECT * FROM questions ORDER BY RAND() LIMIT 20";

thanks in advance!


#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 18 February 2006 - 10:08 PM

That's impossible -- there's absolutely no way that the the statement posted above can ever return the same row twice. That's not to say that you didn't put the same question in the table itself twice -- which seems to be the case if you're getting duplicates.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 masgas

masgas
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 19 February 2006 - 10:05 AM

I don't know why it did so... but I assure you it did! Though I found the way round it like this...

$sql = "SELECT DISTINCT *, id FROM questions ORDER BY RAND() LIMIT 20";

regards...

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 19 February 2006 - 08:46 PM

The fact that you had to use DISTINCT proves that you had the same data in two separate records -- and that should NEVER be the case if you're returning the ID field, since I assume this is the PRIMARY (e.g. UNIQUE) key!
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users