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 9188239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:57:30+00:00 2026-06-17T19:57:30+00:00

Is it possible to write a function within mySQL that checks the value of

  • 0

Is it possible to write a function within mySQL that checks the value of a SELECT query, and if it != 0, continue running the function and add to the result array?

I have a few entries marked as related_id = 0 The rest have actual positive numbers attached.

I am trying to create a single query that will continually loop through a sql function, check and see if the retrieved related_id result is 0. If not, then it will run again, add the newly checked row to the result array and continue until it finds a 0.

The way the DB is set up, a 0 will always occur, and it will be searching via id,so there should only be one result.

I am using a slightly modified tagging system.

keywords_id | keyword  | related_id
------------------------------------
1           | sports   | 0
2           | baseball | 1
3           | football | 1
4           | pitchers | 2

If I pass in related_id = 2 I’m looking to return an array that yields:

array 
[0]
   keyword => 'sports'
   keywords_id => '1'
   related_id => '0'
[1]
   keyword => 'baseball'
   keywords_id => '2'
   related_id => '1'
[2]
   keyword => 'pitcher'
   keywords_id => '4'
   related_id => '2'

I tried my luck with using IF and THEN but i just haven’t seen enough examples and its tough to find solid tutorials online. Thanks!

SELECT k.* 
FROM keywords k
WHERE keywords_id = 2

The above query would essentially start it off. The keywords_id is the related_id that is passed. Therefore the above would return keywords_id: 2 |keyword: baseball |related_id: 1

Because related_id 1 is NOT 0, i would need to run it again.

I realize this can be done in PHP through a few extra queries…is that the way I should do this? I assume it could be done in mySQL after reading about functions. I just couldn’t figure it out.

  • 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-17T19:57:31+00:00Added an answer on June 17, 2026 at 7:57 pm

    MySQL doesn’t support returning an array from a function. Would suggest creating a table (or possibly a temporary table) to hold the results and then writing a stored procedure to populate it instead.

    Not sure exactly what you are trying to achieve but maybe something along the lines of this?

    CREATE TABLE results (keywords_id INT, keyword VARCHAR(2000), related_id INT);
    
    CREATE PROCEDURE populateResults(IN keyword_id_in INT)
    BEGIN
      DECLARE num_results INT;
      DECLARE max_results INT;
      DECLARE next_keyword_id INT;
      SET next_keyword_id = keyword_id_in;
      SET max_results = 5;
      SET num_results = 0;
    
      DELETE FROM results;
    
      the_loop: LOOP
        INSERT INTO results
        SELECT keywords_id, keyword, related_id
        FROM keywords
        WHERE keywords_id = next_keyword_id;
    
        SELECT related_id
        FROM keywords
        WHERE keywords_id = next_keyword_id
        INTO next_keyword_id;
    
        IF (next_keyword_id = 0) || (num_results >= max_results) THEN
          LEAVE the_loop;
        END IF;
        SET num_results = num_results + 1;
      END LOOP the_loop;
    END
    

    See SQL Fiddle Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to write an R function that evaluates an expression within a
Is it possible to write individual code for each result within an .each() function?
Is it possible to write this query in codeigniter active records? $Main_Nav_Query = mysql_query(SELECT
I'm looking to write a function that takes the integers within a list, such
How can I write (if it's possible at all...) a function which takes an
Is it possible for me to use the writeImage function in Imagick to write
So I wrote this function that is given possible numbers, and it has to
Is it possible to write GLSL ES fragment shaders under iOS that generate multiple
I'm trying to write a regex function that will identify and replace a single
I am trying to write a function that will take an xml object, an

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.