There are two columns in the new table(table1 join table2) which have the same column name, how to get the values of both respectively

前端 未结 4 972
有刺的猬
有刺的猬 2021-01-26 19:05
select * from table1 join table2 on table1.column3=table2.column4 where ...
...
$row=mysql_fetch_assoc($result);

However, there are two columns in the

4条回答
  •  不知归路
    2021-01-26 19:38

    When there's a column name collision due to a query joining 2+ tables, you can not use *. You can use:

    SELECT table_1.*, 
           table_2.*
      FROM table_1, 
           table_2
    

    If that doesn't return the list of columns you want, you will have to explicitly list every column. There's no way around - it's an all or nothing deal.

    Table Aliases

    ...but typing out the full table name every time can be a pain, which is why you can alias tables:

    SELECT t1.*, 
           t2.*
      FROM table_1 AS t1, 
           table_2 t2
    

    Either alias notation is supported. They're also required if you want to join a table to itself.

提交回复
热议问题