Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8068445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:41:23+00:00 2026-06-05T12:41:23+00:00

Update: I fixed the previous problems. Now the codes are up-dated. Results are unique

  • 0

Update:

I fixed the previous problems. Now the codes are up-dated. Results are unique and IDs are right. But new problem:
The amount of result rows is often less than requirement (8).
Because I added CREATE UNIQUE INDEX topicid on rands (topicid); to deny the repeated inserts in SQL layer; the loop – 1 regardless the insert is denied. I am now looking for a method like: IF insert successful THEN cnt-=1. Do you know any way to do this in SQL layer? Thanks.


I have a table called topictable which contains two coulmns-topicid and topic. I want to get 8 random rows from the table without repeats. I stole the code from here and modified into getting two column results. But I have two problems with it. 1. It is not distinct; 2. the id is wrong (I catch the wrong random id somehow).

DELIMITER $$
DROP PROCEDURE IF EXISTS get_rands$$
CREATE PROCEDURE get_rands(IN cnt INT)
BEGIN
  DROP TEMPORARY TABLE IF EXISTS rands;
  CREATE TEMPORARY TABLE rands ( topicid INT ,topic VARCHAR(128) );
  CREATE UNIQUE INDEX topicid on rands (topicid);
loop_me: LOOP
    IF cnt < 1 THEN
      LEAVE loop_me;
    END IF; 
    INSERT INTO rands 
       SELECT topictable.topicid,topictable.topic
         FROM topictable 
         JOIN (SELECT (RAND()*(SELECT MAX(topictable.topicid) FROM topictable)) AS id) AS choices
        WHERE topictable.topicid >= choices.id
        LIMIT 1;
    SET cnt = cnt - 1;
  END LOOP loop_me;  
END$$
DELIMITER ;

I use the following function in php to execute the script.

    function pickrandomtopics($amountoftopics,$dbh){
try {  
    $randtable="CALL get_rands($amountoftopics)";
    $dbh->exec("$randtable");
    $topictemp = $dbh->query('SELECT * FROM rands');
    $topics = $topictemp->fetchAll(PDO::FETCH_ASSOC);
}  
catch(PDOException $e) {  
    echo $e->getMessage();  
}
return $topics;}

Thanks for your helps.


Sorry, I didn’t make it clear. All the topics in this table are unique in the beginning. But they might duplicate at the end, because the function picks the row randomly from time to time, sometime it just pick a same row twice. Thanks for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:41:25+00:00Added an answer on June 5, 2026 at 12:41 pm

    Thank @tereško. the CONTINUE HANDLER FOR SQLSTATE '23000' catches “not unique” errors and correct cnt back by +1.

    DELIMITER $$
    DROP PROCEDURE IF EXISTS get_rands$$
    CREATE PROCEDURE get_rands(IN cnt INT)
    BEGIN
      DECLARE CONTINUE HANDLER FOR SQLSTATE '23000' SET cnt = cnt + 1;
      DROP TEMPORARY TABLE IF EXISTS rands;
      CREATE TEMPORARY TABLE rands ( topicid INT UNIQUE,topic VARCHAR(128) );
    loop_me: LOOP
        IF cnt < 1 THEN
          LEAVE loop_me;
        END IF; 
        INSERT INTO rands 
           SELECT topictable.topicid,topictable.topic
             FROM topictable 
             JOIN (SELECT (RAND()*(SELECT MAX(topictable.topicid) FROM topictable)) AS id) AS choices
            WHERE topictable.topicid >= choices.id
            LIMIT 1;
        SET cnt = cnt - 1;
      END LOOP loop_me;  
    END$$
    DELIMITER ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update: I've fixed up my referencing below, and my problem is now more specific.
UPDATE It looks like this problem has been quietly fixed in iOS 4.3. Up
Updade: Ok so I've fixed my previous problem (Besides the SQLinjection which I'm not
[UPDATE] Fixed it (will answer later when StackOverflow lets me) but basically I installed
I asked this question earlier and thought it was fixed, but it's not. Previous
(This question is following on from my previous problem which has been fixed (Here)
UPDATE - I have fixed some mistakes in the code below and the images
Update: I reported this as a bug to Apple and they fixed it! All
Update: Switching to IIS Express instead of the Visual Studio Development Server fixed the
Recently updated to the RC of MVC4. It fixed a lot of problems, especially

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.