【问题标题】:MySQL Select all columns from one table and some from another tableMySQL 从一个表中选择所有列,从另一个表中选择一些列
【发布时间】:2011-03-30 09:43:38
【问题描述】:

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

【问题讨论】:

    标签: select mysql join


    【解决方案1】:

    只需使用表名:

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

    这将从myTable 中选择所有列,并从otherTable 中选择foobar 列。

    【讨论】:

    • 如果你想使用 count(myTable.*) 它是如何工作的?
    • 你也可以使用别名,所以当你去 select * from tablename as tn 时,你可以写 select tn.* from tablename as tn.
    • MySql DB 不需要为其他表中的字段添加前缀。例如:SELECT table1.*, field1InTable2, field1InTable3, field2InTable3 from table1 join table2 on .... join table3 on ...,工作!
    【解决方案2】:

    我真的需要更多信息,但它会沿着......

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

    【讨论】:

      【解决方案3】:

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

      这应该选择表 1 中的所有列,并且只选择表 2 中由 id 连接的列出的列。

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

        • 除非您要添加新内容,否则请不要回答。 (特别是 8 岁的问题,得到了非常高的评价/足够的答案。)
        猜你喜欢
        • 2019-05-04
        • 2021-12-30
        • 1970-01-01
        • 2016-07-22
        • 2017-11-13
        • 2016-09-08
        • 2023-04-05
        • 1970-01-01
        • 2022-01-05
        相关资源
        最近更新 更多