Good day. Faced such questions.
Create a simple query with data selection from mysql
SELECT name, name2 FROM table
As a result, I output
$ row ['name']
$ row ['name2']
After that, a more complex query with many-to-many relationships is created and you have to use this option
SELECT t1. *, t2. * FROM table1 AS t1
INNER JOIN table2 AS t2 ON t1.id = t2.id
...
And now the question! If you write the output in this way:
$ row ['t1.name']
$ row ['t2.name2']
Nothing will happen. And that’s understandable.
Is it possible to somehow refer to a table and then to a cell for output?
Or the simplest and perhaps the only solution is to also assign “abbreviation” to cells
SELECT t1.name as name, t2.name2 as name2 FROM table1 AS t1
INNER JOIN table2 AS t2 ON t1.id = t2.id
...
$ row ['name']
$ row ['name2']
Answer 1, authority 100%
References to a table like & lt; table_name & gt;. & lt; field_name & gt;
work within a query. When the query result is generated, the columns in it have only the field name. The table-source of data after the formation of the result can no longer be defined. Therefore, specify the names of the requested columns explicitly. SELECT *
is generally bad practice if you know the fields you want to get.
I just don’t understand why you need alias. This:
SELECT t1.name as name, t2.name2 as name2 FROM table1 AS t1
INNER JOIN table2 AS t2 ON t1.id = t2.id
and this:
SELECT t1.name, t2.name2 FROM table1 AS t1
INNER JOIN table2 AS t2 ON t1.id = t2.id
will give the same result. The real meaning in aliases will appear if two tables have a column with the same name. For example:
SELECT t1.name as name, t2.name as name2 FROM table1 AS t1
INNER JOIN table2 AS t2 ON t1.id = t2.id
Answer 2
It won’t be too easy. Either each repeated field is named AS, or unique column names are required.
Alternatively, check the request. UNION ALL may be suitable for your case (in the manual)