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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:00:01+00:00 2026-05-23T00:00:01+00:00

I have a table with the following structure: (table_name, column_name) and for each row

  • 0

I have a table with the following structure:

(table_name, column_name) 

and for each row in this table I need to query the column_name in the table_name and do a COUNT(column_name) GROUP BY column_name of the values in there.

Currently I do

SELECT * FROM this table 
/*and then*/ foreach row: do another query with: 
  SELECT column_name, COUNT(column_name) GROUP BY column_name

Is there any way I can do this in a single query?
Something like

SELECT column_name, COUNT(column_name) 
GROUP BY column_name 
FOREACH(SELECT table_name, column_name FROM my_initial_table)

I know the last query is not valid, it’s just an example for what I am looking to to achieve if it’s possible.

LE:

The table that tells me where to look has 2 varchar columns

Ex:

|++++++++++++++++++++++++++++++++
| table_name | column_name      |
|+++++++++++++++++++++++++++++++|
| logsa      | foo              |
|===============================|
| logsa      | bar              |
|===============================|
| testx      | baz              |
|===============================|

This tells me that now i have too look in columns foo and bar of table logsa and column baz of table testx

Every column in every table has VARCHAR as datattype and i just need to count those that are the same. that’s way i was doing

SELECT column_name, COUNT(column_name) 
    GROUP BY column_name
  • 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-05-23T00:00:02+00:00Added an answer on May 23, 2026 at 12:00 am

    If you are working in MySql, you can’t directly use parametrized column names.
    There is an indirect way of doing this using stored procedures and prepared statements.

    some sloppy first-draft code…
    notice the difference between backticks ` and quotes ‘

      CREATE PROCEDURE CountTables()
      BEGIN
          DECLARE done TINYINT DEFAULT 0;
          DECLARE table_name varchar(30), colunn_name varchar(30);
          DECLARE cur1 CURSOR FOR SELECT
              table_name, column_name
           FROM ColumnTable;
    
          DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
          CREATE TEMPORARY TABLE t1( table_name varchar(30), column_name varchar(30), count int);
    
          OPEN cur1;
          START TRANSACTION;
    
        read_loop: LOOP
            FETCH FROM cur1 INTO table_name, column_name;
            IF done THEN LEAVE read_loop; END IF;
    
            SET insert_sql = CONCAT(  "INSERT INTO `t1`(`table_name`, `column_name`, `count`) SELECT ",
                                       "'", table_name, "', '", column_name, "', count(`", column_name, "`)",
                                      " FROM `",    table_name, "`"  
                                   );
    
            PREPARE insert_stmt FROM insert_sql;
            EXECUTE insert_stmt;
            DEALLOCATE PREPARE insert_stmt;
    
          END LOOP;
          COMMIT;
          SELECT * FROM t1 GROUP BY column_name;
          DROP TEMPORARY TABLE t1;
    
        END;
    

    Oh ya, don’t forget to call the procedure:

    CALL CountTables();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have already googled for this I have a Table with following structure in
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I have the following table structure CREATE TABLE `table` ( `id` int(11) NOT NULL
I have a table with a structure like the following: LocationID AccountNumber long-guid-here 12345
I have the following table structure. I want to select distinct CustomerId and CustomerName
I have created a table in MSSQL having following structure - **Table Name -
I have a database with the following structure: CREATE TABLE entity ( id SERIAL,
I have the following requirement. Ex: There is a transaction table where it has
I have a tree-style database with the following structure: Table fields: NodeID int ParentID
I have 2 tables with the following structure.. category { sub_cat_id - numeric sub_cat_name

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.