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

4

10

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.

Rozanne answered 19/5, 2011 at 23:50 Comment(2)
50 columns in a single table? might want to look at modulizing thatPiccoloist
I'd assume 50 columns across multiple tables.Tablespoon
A
15

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.

Akel answered 19/5, 2011 at 23:58 Comment(0)
Y
0

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.

Yokefellow answered 19/5, 2011 at 23:56 Comment(0)
I
0

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)

Inamorato answered 21/5, 2011 at 2:9 Comment(0)
B
0

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.

Bedabble answered 1/2, 2012 at 11:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.