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

  • Home
  • SEARCH
  • 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 9024773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:06:30+00:00 2026-06-16T06:06:30+00:00

My Procedure: (It’s a scheme) delimiter // CREATE PROCEDURE get_random_data() BEGIN Declare Count_a INT;

  • 0

My Procedure: (It’s a scheme)

  delimiter //
  CREATE PROCEDURE get_random_data()
  BEGIN
  Declare Count_a INT;
      SELECT Floor(Count(`id_x`) * Rand()) INTO Count_a From `x_data`  Where `ok_w` = 1 And `active` = 2 And `time` >= Unix_timestamp();

   # Now We select Some Rows
   SELECT `id_x` From `x_data` Where `spec` = 1 Limit Count_a,3;

   # And if Count of this select is small than < 3

   IF(row_count() < 3) then
      SELECT `id_anonsu` From `x_data` Where `spec` = 0 Limit Count_a,2;
   END IF ;


  END//
  delimiter ;

// Target

How to get the results From BOTH queries?
// One from query before if statement

// Second from query in statement, if first query count is smaller than 3

  • 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-16T06:06:31+00:00Added an answer on June 16, 2026 at 6:06 am

    Ok, I found the answer.

    The original code:

       IF(row_count() < 3) then
           SELECT `id_anonsu` From `x_data` Where `spec` = 0 Limit Count_a,2;
       END IF ;
    

    Was replaced for:

    IF(FOUND_ROWS() < 3) THEN SELECT `id_anonsu` From `x_data` Where `spec` = 0 Limit Count_a,2;
    END IF ;
    

    Better Version:

    Work Good with query (Who use LIMIT)
    And we need know number of returned rows by (Query)in Store Procedure

    Version with show count

        DROP PROCEDURE IF EXISTS test;
        Delimiter //
        CREATE PROCEDURE test(OUT w INT)
        BEGIN
        Declare Counter_a INT DEFAULT 0;
    
    
            Set @id_counter = 0;
    
            SELECT Floor(Count(`id_x`) * Rand()) INTO Counter_a From `x_data`  Where `ok_w` = 1 And `active` = 2 And `time` >= Unix_timestamp();
    
            SELECT @id_counter := @id_counter + 1, `id_x` From `x_data` Where `spec` = 1 Limit Counter_a,3;
    
    
            IF @id_counter < 3 THEN 
                Set Counter_a = Counter_a + 1;
                SELECT `id_x` From `x_data` Limit Counter_a,2;
    
    
            END IF;
    
            Set w = @id_counter;
    
    
        END //
        Delimiter ;
    

    Output like this:

      mysql> Call test(@z); Select @z;
      +--------------------------------+-----------+
      | @id_counter := @id_counter + 1 |   id_x    |
      +--------------------------------+-----------+
      |                              1 |         7 |
      |                              2 |         8 |
      +--------------------------------+-----------+
      2 rows in set (0.00 sec)
    
      +-----------+
      |   id_x    |
      +-----------+
      |         8 |
      +-----------+
      1 row in set (0.01 sec)
    
      Query OK, 0 rows affected (0.01 sec)
    
      +------+
      | @z   |
      +------+
      |    2 |
      +------+
      1 row in set (0.00 sec)
    


    Version with hidden count

        DROP PROCEDURE IF EXISTS test;
        Delimiter //
        CREATE PROCEDURE test(OUT w INT)
        BEGIN
        Declare Counter_a INT DEFAULT 0;
    
    
            Set @id_counter = 0;
    
            SELECT Floor(Count(`id_x`) * Rand()) INTO Counter_a From `x_data`  Where `ok_w` = 1 And `active` = 2 And `time` >= Unix_timestamp();
    
            SELECT `id_x` From `x_data` Where @id_counter := @id_counter + 1 And `spec` = 1 Limit Counter_a,3;
    
    
            IF @id_counter < 3 THEN 
                Set Counter_a = Counter_a + 1;
                SELECT `id_x` From `x_data` Limit Counter_a,2;
    
    
            END IF;
    
            Set w = @id_counter;
    
    
        END //
        Delimiter ;
    

    Output like this:

      mysql> Call test(@z); Select @z;
      +-----------+
      |   id_x    |
      +-----------+
      |         7 |
      |         8 |
      +-----------+
      2 rows in set (0.00 sec)
    
      +-----------+
      |   id_x    |
      +-----------+
      |         8 |
      +-----------+
      1 row in set (0.01 sec)
    
      Query OK, 0 rows affected (0.01 sec)
    
      +------+
      | @z   |
      +------+
      |    2 |
      +------+
      1 row in set (0.00 sec)
    

    @TIP

     @id_counter := @id_counter + 1
    

    It’s Must be before all conditions in where.

    For example:

    [.....] Where @id_counter := @id_counter + 1 And `field_2` > 1 LIMIT 0, 5;
    

    Will return always 1…


    Correct Version

    [.....] Where `field_2` > 1 And  [Other conditions] And @id_counter := @id_counter + 1  LIMIT 0, 5;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Create procedure count_loop( p_start int, p_stop int, p_step int unsigned, p_delim varchar(5)) begin declare
Create procedure a06_generate_data( p_loop_count int, p_min int, p_max int, p_status char(1)) begin declare v_max
Procedure delimiter $$ drop procedure if exists db1.test; create procedure db1.test() deterministic begin select
CREATE procedure St_Proc_GetUserReportforCurrentDayTask @userID int as Begin set NoCount on; DECLARE @TODAY DATE SET
CREATE PROCEDURE SortModules @flag int AS BEGIN CASE @flag WHEN 0 THEN SELECT FriendlyName,ModuleID
My procedure is: create procedure news as select newsdate,COUNT(B.id) as total from news B
ALTER PROCEDURE GetSingersGenere (@SingerData ntext) AS BEGIN DECLARE @hDoc int exec sp_xml_preparedocument @hDoc OUTPUT,@SingerData
CREATE PROCEDURE [test].[proc] @ConfiguredContentId int, @NumberOfGames int AS BEGIN SET NOCOUNT ON RETURN @WunNumbers
CREATE PROCEDURE Pname(in_empno IN NUMBER out_name OUT VARCHAR2) AS BEGIN select EmpName into out_name
CREATE PROCEDURE GetHandTypes() BEGIN SELECT * FROM Codemst WHERE code LIKE HT% AND dep

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.