MySQL从一个表中选择所有列,而从另一个表中选择一些列

时间:2022-09-21 15:17:06

How do you select all the columns from one table and just some columns from another table using JOIN? In MySQL.

如何使用JOIN从一个表中选择所有列,只从另一个表中选择一些列?在MySQL。

4 个解决方案

#1


323  

Just use the table name:

只需要使用表名:

SELECT myTable.*, otherTable.foo, otherTable.bar...

That would select all columns from myTable and columns foo and bar from otherTable.

这将从myTable中选择所有列,从otherTable中选择foo和bar列。

#2


32  

I need more information really but it will be along the lines of..

我确实需要更多的信息,但这将取决于……

SELECT table1.*, table2.col1, table2.col3 FROM table1 JOIN table2 USING(id)

#3


1  

select a.* , b.Aa , b.Ab,b.Ac from table1 a left join table2 b on a.id=b.id

选择一个。*,b。Aa,b.Ab,b。从表1 a左连接表2 b, a id=b id

this should select all columns from table 1 and only the listed columns from table 2. joing by id.

这将从表1中选择所有列,并且只选择表2中列出的列。智合科技通过id。

#4


1  

Using alias for referencing the tables to get the columns from different tables after joining them.

使用别名引用表,以便在加入表后从不同的表中获取列。

Select tb1.*, tb2.col1, tb2.col2 from table1 tb1 JOIN table2 tb2 on tb1.Id = tb2.Id

#1


323  

Just use the table name:

只需要使用表名:

SELECT myTable.*, otherTable.foo, otherTable.bar...

That would select all columns from myTable and columns foo and bar from otherTable.

这将从myTable中选择所有列,从otherTable中选择foo和bar列。

#2


32  

I need more information really but it will be along the lines of..

我确实需要更多的信息,但这将取决于……

SELECT table1.*, table2.col1, table2.col3 FROM table1 JOIN table2 USING(id)

#3


1  

select a.* , b.Aa , b.Ab,b.Ac from table1 a left join table2 b on a.id=b.id

选择一个。*,b。Aa,b.Ab,b。从表1 a左连接表2 b, a id=b id

this should select all columns from table 1 and only the listed columns from table 2. joing by id.

这将从表1中选择所有列,并且只选择表2中列出的列。智合科技通过id。

#4


1  

Using alias for referencing the tables to get the columns from different tables after joining them.

使用别名引用表,以便在加入表后从不同的表中获取列。

Select tb1.*, tb2.col1, tb2.col2 from table1 tb1 JOIN table2 tb2 on tb1.Id = tb2.Id