Select only some columns from a table on a LEFT JOIN

前端 未结 3 416
野性不改
野性不改 2021-02-05 06:20

Is it possible to select only some columns from a table on a LEFT JOIN?

相关标签:
3条回答
  • 2021-02-05 06:55

    If you want some of table1's columns and some of table2's columns, you would do something like

    SELECT t1.col1, t1.col2, t1.col3, t2.col1, t2.col2, t2.col3
    FROM table1 t1
    LEFT JOIN table2 t2
    ON...
    
    0 讨论(0)
  • 2021-02-05 07:02

    Of course. Just list the columns you want to select as you would in any query:

    SELECT table1.column1, table1.column2, table2.column3
    FROM table1
    LEFT JOIN table2 ON (...)
    

    Note that I've included the table1. or table2. prefix on all columns to be sure there aren't any ambiguities where fields with the same name exist in both tables.

    0 讨论(0)
  • 2021-02-05 07:10

    Add a * to just that table in your select statement, separate from other columns with a comma:

    SELECT table1.*, table2.col2, table2.col3
    FROM table1
    LEFT JOIN table2
    ON...
    

    Source: https://stackoverflow.com/a/3492919/3417198

    0 讨论(0)
提交回复
热议问题