【问题标题】:SQLite: join selected columns from two tablesSQLite:连接两个表中的选定列
【发布时间】:2017-04-24 02:41:51
【问题描述】:

我正在尝试创建一个新的 SQLite 表,仅将一个表中的选定列与第二个表中的所有列合并。这两个表只共享一列(一个表的主键和第二个表的外键)。

create temp tablex as select column 1, column2 from table1 natural join table2;

连接运行,但结果表只有表 1 中的列 - 没有表 2 中的列。 我尝试使用 table2 的子查询:

create temp tablex as select column 1, column2 from table1 natural join select * from table2;

create temp tablex as select column 1, column2 from table1 natural join (select * from table2);

(以及一些正确的尝试),但这些都给了我一个语法错误“接近选择”。 如果我提前创建 table1 的列子集并在没有任何“选择”的情况下自然合并生成的两个表,我可以很好地进行合并。但我认为我应该能够在单个 SQL 语句中做到这一点。 我究竟做错了什么? (如有必要,我可以创建一个模型,但我假设我错过了正确语法的一点)。 在此先感谢您的帮助。 拉里·亨斯克

【问题讨论】:

  • 原表的列有哪些?您希望结果中包含哪些列?

标签: mysql sqlite select join


【解决方案1】:

是的。这行得通。

create temp table a as select b.col1, b.col2, c.* from table1 as b natural join table2 as c;

非常感谢。

【讨论】:

    【解决方案2】:
    create temp tablex as select table1.column1, table1.column2, table2.column3, table2.column4 from table1, table2 where table1.PrrimaryKey = table2.ForiegnKey;
    

    你可以这样做。

    【讨论】:

    • 这是解决方案吗?如果是,请标记为这样。
    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 2014-12-08
    • 2019-04-09
    • 2016-08-02
    • 2021-11-08
    • 2020-04-04
    • 2021-05-17
    • 1970-01-01
    相关资源
    最近更新 更多