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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:31:35+00:00 2026-05-19T03:31:35+00:00

I need to SELECT from more than one table that has a lot of

  • 0

I need to SELECT from more than one table that has a lot of fields. Do I have to manually fabricate the query like this:

table1.field1 AS table1_field1, table1.field2 AS table1_field2, ..., table2.field1 AS table2_field1, ...

Or is there any workaround for this in MySQL or PHP that will automatically take care of all these when there are identical field names in the tables?

In PHP, I tried:

$results = $conn -> query("SELECT table1.*, table2.* FROM table1, table2 WHERE 1"); // $conn is a mysqli object
$results = $results -> fetch_array();

But if there is a field named ‘title’ in both of the 2 tables, $results[‘title’] would just contain the value of the record from the 2nd table.

I know there are numeric indexes that you can use to access the value but:

  1. It’s unreadable.
  2. You have to update the code accordingly every time the table structure such as the order of the fields changes.

Any idea?

  • 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-19T03:31:36+00:00Added an answer on May 19, 2026 at 3:31 am

    Yes, there are workarounds.
    mysql and mysqli extensions, as well as certain pdo drivers, have functions to let you get at column meta data. Heres a pdo sample

    $stmt->execute();
    $qualifiedColumnNames = array();
    for ($i = 0; $i < $stmt->columnCount(); $i++) {
        $columnMeta = $stmt->getColumnMeta($i);
        $qualifiedColumnNames[] = "$columnMeta[table].$columnMeta[name]";
    }
    
    print_r(array_combine($qualifiedColumnNames, $stmt->fetch(PDO::FETCH_NUM)));
    

    So you can use table1.* and be able to differentiate conflicting column names.

    Also, another trick that works because the column order in the result set is deterministic.

    select 'table1' table_sep1
           table1.*
           'table1' table_sep2
           table2.*
    

    I’ll leave the code up to you, but you can fetch a row as an array, and loop through the array keys. Since they will be in order, you can assume each column that follows a table separator column belongs to the table specified by the separator, until you come across the next sep.

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

Sidebar

Related Questions

I need a select from table which does not have column that tells when
I have need to select a number of 'master' rows from a table, also
I'd like to select all rows from one table which match one or more
I have a <select>. Using JavaScript, I need to get a specific <option> from
So I have a table that's basically a tree. Each row has a parent
I have a query that retrieves the name of each friend a user has
I have inherited a Access database that has a query that SELECTs over 50
I need to prevent user from selecting text (select all or select a portion
I need a quick algorithm to select 5 random elements from a generic list.
I need to select a bunch of data into a temp table to then

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.