I want to select all columns from a table with one or more joins, but I want to be able to distinguish which table the columns belong to (prepend the column names with the table name). I won't know what columns are in each table so I can't list them manually.
users:
--------------------------------
| id | name | age | contact_id |
--------------------------------
| 18 | Foo | 21 | 1 |
| 19 | Bar | 32 | 2 |
contacts:
----------------------------
| id | address | phone |
----------------------------
| 1 | 123 Main | 867-5309 |
| 2 | 987 Wall | 555-5555 |
I want to something with:
SELECT * FROM users
JOIN contacts on users.contact_id = contacts.id
WHERE users.id = 18
And get a result like:
---------------------------------------------------------------------
| users.id | users.name | ... | contacts.id | contacts.address | ...
---------------------------------------------------------------------
| 18 | Foo | ... | 1 | 123 Main | ...
Attempts
Thus far I've found this to get the column names for each table:
SELECT concat('contacts.', `COLUMN_NAME`) AS 'contacts'
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='myDb'
AND `TABLE_NAME`='contacts'
But I don't know how to put that back into my selects or if that's even the right path to go.
Success: My implementation of @Bernd Buffen's answer:
I'm using PHP's PDO class to get my results, so here's how I did it. (I'm ignoring the contacts table for this example as it's easy enough to extrapolate.)
$pdo = new PDO(...);
$stmt = $pdo->prepare(<<< EOT
SELECT group_concat(" ", COLUMN_NAME, " AS 'users.", COLUMN_NAME, "'") as 'columns'
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='myDb'
AND `TABLE_NAME`='users'
EOT);
$stmt->execute();
$columns = $stmt->fetch()['columns'];
$stmt2 = $pdo->prepare("SELECT $columns FROM users WHERE id = 18 LIMIT 1");
$stmt2->execute();
$result = $stmt2->fetch();