Good day. Faced with such questions.

I create a simple query with a choice of data from mysql

SELECT name, name2 FROM table 

As a result, I deduce

 $row['name'] $row['name2'] 

After that, a more complex query with many-to-many connections 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 register in this way the output:

 $row['t1.name'] $row['t2.name2'] 

Nothing will happen. And this is 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 also to assign a "reduction" for the 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'] 

    2 answers 2

    References to the table of the form <имя_Ρ‚Π°Π±Π»ΠΈΡ†Ρ‹>.<имя_поля> work within the query. When the result of the query is formed, the columns in it have only the name of the field. The data source table after the formation of the result is no longer defined. Therefore, specify the names of the requested columns explicitly. SELECT * is generally a bad practice, unless of course the fields you want to receive are known. Just do not understand why you alias-s. It:

     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 pseudonyms will appear if there is a column with the same name in two tables. 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 
    • In the example, I gave all the wrong and incorrect names and there is no use needed. In my case, there are three tables in the queries in which there are identical fields. But I heard you thank you! So my logic was correct. And yes * I do not use once =) - Deniss
    • The only thing that strains, the length of the request due to the enumeration of the fields will not affect its processing speed? - Deniss
    • @Deniss * the same enumeration of fields, only the database chooses them from the table structure itself, so if your enumeration friend chooses not all fields, but only necessary ones, then maybe it will be even a little faster, though there’s any difference in speed out of that is vanishingly small - Mike

    It will not work too easy. Either each duplicate field is called AS, or unique column names are required. As option check request. May be suitable for your case UNION ALL (in the manual)