I'm currently debugging a huge MySql call which joins a large amount of tables which share column names such as id, created_at, etc. I started picking it apart but I was wondering if there was a way to do something like:
SELECT * AS table.column_name FROM table1 LEFT JOIN etc etc etc...
In place of having to individually name columns like:
SELECT table1.`column2' AS 'NAME', table1.`column3` AS ...
It would definitely help with speeding up the debugging process if there's a way to do it.
Thanks.
Edit:
Thanks for the answers so far. They're not quite what i'm looking for and I think my question was a bit vague so i'll give an example:
Suppose you have this setup in your MySql Schema:
table: students fields: INT id | INT school_id | VARCHAR name
table: schools fields: INT id | INT name
students contains:
1 | 1 | "John Doe"
schools contains:
1 | "Imaginary School One"
Doing the MySql call "SELECT * FROM students LEFT JOIN schools ON (students.school_id = schools.id)" will yield:
id | school_id | name | id | name
1 | 1 | "John Doe" | 1 | "Imaginary School One"
We know better and we know that the first Id and Name columns refer to the students table and the second Id and Name refer to the schools table since the data set is really small and unambiguous with its naming. However, if we had to deal with a result set that contained multiple left joins and columns with similar names, then it would start to get difficult to read and normally you'd have to trace through it by following the joins. We could start doing something like
SELECT school.name AS 'school_name', etc etc etc...
But that gets incredibly impractical once you start dealing with large data sets.
I was wondering though if there was a way to return the result set wherein the column names would look like this instead:
students.id | students.school_id | students.name | schools.id | schools.name
Which would be useful for future references if I need to do something similar again.
You can list a table's columns with the mysqlshow db_name tbl_name command. The DESCRIBE statement provides information similar to SHOW COLUMNS .
The more flexible way to get a list of columns in a table is to use the MySQL SHOW COLUMNS command. As you can see the result of this SHOW COLUMNS command is the same as the result of the DESC statement.
What if you select the tables in order, and add a spacer column with the name.
i.e.
select 'table1', t1.*, 'table2', t2.*, 'table3', t3.*
...
At least that way you don't have to name specific columns.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With