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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:36:46+00:00 2026-06-02T17:36:46+00:00

Supposing a query such as: SELECT * FROM tableA; How can I prepend a_

  • 0

Supposing a query such as:

SELECT * FROM tableA;

How can I prepend a_ to each columns’ name? For example if there is a column “username” it would be accessed in the results as “a_username”.

EDIT: The SELECT username AS a_username format will not help as I need to continue using the * field selection. There is a JOIN and a potential conflict with a returned column from another table in the JOIN. I will be iterating over the returned columns (foreach) and only want to output the columns that came from a particular table (whose schema may change) to HTML input fields where a site admin could edit the fields’ content directly. The SQL query in question looks like SELECT firstTable.*, anotherTable.someField, anotherTable.someOtherField and their exists the possibility that someField or someOtherField exists in firstTable.

Thanks.

  • 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-02T17:36:48+00:00Added an answer on June 2, 2026 at 5:36 pm

    You can use the INFORMATION_SCHEMA.COLUMNS table to formulate the query and then use dynamic SQL to execute it.

    First let’s make a sample database called dotancohen and a table called mytable

    mysql> drop database if exists dotancohen;
    Query OK, 1 row affected (0.03 sec)
    
    mysql> create database dotancohen;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> use dotancohen
    Database changed
    mysql> create table mytable
        -> (
        ->     id int not null auto_increment,
        ->     username varchar(30),
        ->     realname varchar(30),
        ->     primary key (id)
        -> );
    Query OK, 0 rows affected (0.06 sec)
    
    mysql> insert into mytable (realname,username) values
        -> ('rolando','odnalor'),('pamela','alemap'),
        -> ('dominique','euqinimod'),('diamond','dnomaid');
    Query OK, 4 rows affected (0.05 sec)
    Records: 4  Duplicates: 0  Warnings: 0
    
    mysql> select * from mytable;
    +----+-----------+-----------+
    | id | username  | realname  |
    +----+-----------+-----------+
    |  1 | odnalor   | rolando   |
    |  2 | alemap    | pamela    |
    |  3 | euqinimod | dominique |
    |  4 | dnomaid   | diamond   |
    +----+-----------+-----------+
    4 rows in set (0.00 sec)
    
    mysql>
    

    Here is the metadata table called INFORMATION_SCHEMA.COLUMNS:

    mysql> desc INFORMATION_SCHEMA.COLUMNS;
    +--------------------------+---------------------+------+-----+---------+-------+
    | Field                    | Type                | Null | Key | Default | Extra |
    +--------------------------+---------------------+------+-----+---------+-------+
    | TABLE_CATALOG            | varchar(512)        | NO   |     |         |       |
    | TABLE_SCHEMA             | varchar(64)         | NO   |     |         |       |
    | TABLE_NAME               | varchar(64)         | NO   |     |         |       |
    | COLUMN_NAME              | varchar(64)         | NO   |     |         |       |
    | ORDINAL_POSITION         | bigint(21) unsigned | NO   |     | 0       |       |
    | COLUMN_DEFAULT           | longtext            | YES  |     | NULL    |       |
    | IS_NULLABLE              | varchar(3)          | NO   |     |         |       |
    | DATA_TYPE                | varchar(64)         | NO   |     |         |       |
    | CHARACTER_MAXIMUM_LENGTH | bigint(21) unsigned | YES  |     | NULL    |       |
    | CHARACTER_OCTET_LENGTH   | bigint(21) unsigned | YES  |     | NULL    |       |
    | NUMERIC_PRECISION        | bigint(21) unsigned | YES  |     | NULL    |       |
    | NUMERIC_SCALE            | bigint(21) unsigned | YES  |     | NULL    |       |
    | CHARACTER_SET_NAME       | varchar(32)         | YES  |     | NULL    |       |
    | COLLATION_NAME           | varchar(32)         | YES  |     | NULL    |       |
    | COLUMN_TYPE              | longtext            | NO   |     | NULL    |       |
    | COLUMN_KEY               | varchar(3)          | NO   |     |         |       |
    | EXTRA                    | varchar(27)         | NO   |     |         |       |
    | PRIVILEGES               | varchar(80)         | NO   |     |         |       |
    | COLUMN_COMMENT           | varchar(1024)       | NO   |     |         |       |
    +--------------------------+---------------------+------+-----+---------+-------+
    19 rows in set (0.02 sec)
    
    mysql>
    

    What you need from this table are the following columns:

    • table_schema
    • table_name
    • column_name
    • ordinal_position

    What you are asking for is to have the column_name and the column_name prepended with a_

    Here is the query and how to execute it:

    select concat('select ',column_list,' from ',dbtb) into @newsql
    from (select group_concat(concat(column_name,' a_',column_name)) column_list,
    concat(table_schema,'.',table_name) dbtb from information_schema.columns
    where table_schema = 'dotancohen' and table_name = 'mytable'
    order by ordinal_position) A;
    select @newsql;
    prepare stmt from @newsql;
    execute stmt;
    deallocate prepare stmt;
    

    Let’s execute it

    mysql> select concat('select ',column_list,' from ',dbtb) into @newsql
        -> from (select group_concat(concat(column_name,' a_',column_name)) column_list,
        -> concat(table_schema,'.',table_name) dbtb from information_schema.columns
        -> where table_schema = 'dotancohen' and table_name = 'mytable'
        -> order by ordinal_position) A;
    Query OK, 1 row affected (0.01 sec)
    
    mysql> select @newsql;
    +--------------------------------------------------------------------------------+
    | @newsql                                                                        |
    +--------------------------------------------------------------------------------+
    | select id a_id,username a_username,realname a_realname from dotancohen.mytable |
    +--------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> prepare stmt from @newsql;
    Query OK, 0 rows affected (0.00 sec)
    Statement prepared
    
    mysql> execute stmt;
    +------+------------+------------+
    | a_id | a_username | a_realname |
    +------+------------+------------+
    |    1 | odnalor    | rolando    |
    |    2 | alemap     | pamela     |
    |    3 | euqinimod  | dominique  |
    |    4 | dnomaid    | diamond    |
    +------+------------+------------+
    4 rows in set (0.01 sec)
    
    mysql> deallocate prepare stmt;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql>
    

    Give it a Try !!!

    You mentioned in your question : The SELECT username AS a_username format will not help as I need to continue using the * field selection.

    All you have to do to implement my suggestion is run the query using tableA as follows:

    select concat('select ',column_list,' from ',dbtb) into @newsql
    from (select group_concat(concat(column_name,' a_',column_name)) column_list,
    concat(table_schema,'.',table_name) dbtb from information_schema.columns
    where table_schema = DATABASE() and table_name = 'tableA'
    order by ordinal_position) A;
    

    When you retrieve that query result, just use it as the query to submit to mysql_query.

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

Sidebar

Related Questions

Supposing I want to query for the XPath //*[@id=$href] . How can I tell
Short version: How do I write this query in squeel? SELECT OneTable.*, my_count FROM
Suppose following codes: IEnumerable<MyClass> MakeQuery() { var query = from m in session.Linq<MyClass>() select
Supposing I have this: foo bar 1 and foo bar 2 How can I
Supposing that I have millions of user profiles, with hundreds of fields (name, gender,
The HTML5 spec mentions an UndoManager that can be used to query the previous
supposing that I have two tables with same 3 columns: ID (int, identity), text
Is there a way of changing the Fixed Keyword Query in the search results
I have a query request-uri in the form of /node/143 (just an example of
Supposing I've got a bit of SQL that filters data by a column which

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.