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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:12:11+00:00 2026-06-09T10:12:11+00:00

How can I UNION all results from stmtQuery to ONE RESULTS example results from

  • 0

How can I UNION all results from stmtQuery to ONE RESULTS example results from table basia and Comments_11 …. etc

    DELIMITER $$
DROP PROCEDURE IF EXISTS SearchUserY $$
CREATE PROCEDURE `SearchUserY`(IN UserIdValue INT(11) )
BEGIN
       DECLARE done INT DEFAULT FALSE;
       DECLARE tableName VARCHAR(50);
       DECLARE stmtFields TEXT ; 
       DECLARE columnName VARCHAR(50) default 'UserId';

        DECLARE cursor1 CURSOR FOR
            SELECT table_name
            FROM information_schema.COLUMNS
            WHERE table_schema = 'comments' 
            AND column_name LIKE '%UserId';

        DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;


        OPEN cursor1;
        read_loop: LOOP
        FETCH cursor1 INTO tableName;
        IF done THEN
            LEAVE read_loop;
        END IF;

      SET stmtFields = CONCAT('`',tableName,'`','.' , columnName ,'=', UserIdValue) ;


     SET @stmtQuery=CONCAT(@sql,'SELECT Nick, Title, Content FROM ' ,'`',tableName,'`', ' WHERE ', stmtFields ) ;
   select @stmtQuery;


        END LOOP;

       PREPARE stmt FROM @stmtQuery ;
       EXECUTE stmt ;

       DEALLOCATE PREPARE stmt;
       CLOSE cursor1;
END

results example (select @stmtQuery):

     SELECT Nick, Title, Content FROM `basia` WHERE `basia`.UserId=0
     SELECT Nick, Title, Content FROM `Comments_11` WHERE `Comments_11`.UserId=0
     ... etc

I want get a one results from all this query but know I got only One results

  • 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-09T10:12:12+00:00Added an answer on June 9, 2026 at 10:12 am

    Generate query in a loop using CONCAT function, add ‘UNION’ or ‘UNION ALL’ clause between them, then execute result query with a prepared statements.

    Solution without cursor:

    SET @resultQuery = NULL;
    SELECT
      GROUP_CONCAT(
        DISTINCT
        CONCAT('SELECT Nick, Title, Content FROM ', table_name, ' WHERE UserId = ', UserIdValue)
        SEPARATOR '\r\nUNION\r\n'
      )
    INTO
      @resultQuery
    FROM
      information_schema.COLUMNS
    WHERE
      table_schema = 'comments' AND column_name LIKE '%UserId';
    
    SELECT @resultQuery;
    

    It will produce result like this:

    SELECT Nick, Title, Content FROM table1 WHERE UserId = 10
    UNION
    SELECT Nick, Title, Content FROM table2 WHERE UserId = 10
    UNION
    SELECT Nick, Title, Content FROM table3 WHERE UserId = 10
    UNION
    SELECT Nick, Title, Content FROM table4 WHERE UserId = 10
    ...
    

    Increase group_concat_max_len variable if needed. It is the maximum allowed result length for the GROUP_CONCAT() function, default value = 1024.

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

Sidebar

Related Questions

I have a query that uses a UNION ALL to return results from two
I have a query formed by an UNION ALL from two tables. The results
I came across a scenario where I had to use Union all, how can
What is the difference between JOIN and UNION ? Can I have an example?
Using Firebird, I want to combine the results of two queries using UNION ALL,
Is it at all possible to combine two different query results into one feed
I can't figure out how to count results from my query because I limit
There are two ways, that I can think of, to obtain similar results from
I want to union the results sets from the following CTEs so that I
Just recieved an error message that said union __anonymous can only be a part

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.