When Selecting everything from MySQL can I rename specific fields and still select everything else as is with the *

StackOverflow https://stackoverflow.com/questions/6066097

  •  06-09-2020
  •  | 
  •  

Question

Hey again guys. So, I'm trying to find out how to select every field across multiple tables in a mysql database and output the resulting table to a .csv file for Excel. I've found the infamous stackoverflow question dealing with .csv ouput and have all the code there. So, I guess what I'm asking is:

Is there a way to do a mysql query that looks something like this?

SELECT * prof.id AS youth_id FROM time time, profiles prof, WHERE foo='bar'

and have it select everything from both (or like 4) tables but select the id as youth_id, or do I have to specify EVERY value that I want if I need to select certain values as another name?

I would like to not have to specify every field as I have like 50+ to output.

I've tried to search but can't really find what I need. I guess if you could point me in the right direction that would be great.

Was it helpful?

Solution

You don't have to specify every field. Instead you can do the following:

SELECT *, prof.id AS youth_id FROM time time, profiles prof

However the above will return all columns from the two tables plus the column you renamed. So if the original two tables have 4 columns total and you rename one, you will get 5 columns in the result set.

OTHER TIPS

Ryan, you can do what you want to. The only catch is, the results will include both youth_id and id with the same values for each. If you want only youth_id & not id, then you'll have to list all the columns you want instead of using '*'. Incidentally, if you have phpmyadmin handy, you could just enter the sql and see the result immediately.

Well, because no other solution seems perfect, I'm going to think outside the box for a sec. If you don't mind making two calls. One could be to the table schema (where the column names are stored), and then you could programmically change names as much as needed (without typing them all). After that, make a second call with the imploded array as its SELECT part.

An example:

<?php
$tables = array ('table1','table2','table3');
$NameAliases = array('table1.id'=>'profid');

foreach ($tables as $table)
{
    $sql = "SHOW COLUMNS FROM $table;";
    $result = mysql_query($sql);

    $columns = array();

    while ($row = mysql_fetch_array($result))
    {
        $search = array_search($table.'.'.$row["Field"],$NameAliases);
        if ($search === true)
        {
            $parts = explode('.',$NameAliases[$search]);
            $columns[] = "$table.{$row["Field"]} AS {$parts[1]}"; //get the side of the dot
        }
        else
        {
            $columns[] = "$table.{$row["Field"]}";
        }
    }
}
$columnstring = implode(', ',$columns);
$sql = "SELECT $columnstring FROM ".implode(', ',$tables)." WHERE 1=1";
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result))
{

}
?>

(No clue if the above works at all)

If you really don't want that column duplication (i.e. getting both id and youth_id) from a

SELECT *, prof.id AS youth_id 
FROM time 
INNER JOIN profiles prof 
   ON time.profile_id = prof.id 
WHERE foo = 'bar'

You can SELECT * from each table except the table with the column you wish to alias:

SELECT time.*, prof.id AS youth_id, prof.forename, prof.surname 
FROM time 
INNER JOIN profiles prof 
   ON time.profile_id = prof.id 
WHERE foo = 'bar'

This will get you all columns from time, and the enumerated ones from profiles. You still have to enumerate all columns in the table you're looking to alias a column in, but at least you don't have to list every single one.

Of course, there's also the argument that you should enumerate all columns anyway in case your schema changes at some point, but that's another story.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top