Sql 插入到。。。选择更多的选择列 谢谢你抽出时间。
我有一个插入到。。。选择setup,但我还希望使用额外的列(我插入的表中不存在的列)进行筛选 例如:Sql 插入到。。。选择更多的选择列 谢谢你抽出时间。,sql,postgresql,select,sql-insert,Sql,Postgresql,Select,Sql Insert,我有一个插入到。。。选择setup,但我还希望使用额外的列(我插入的表中不存在的列)进行筛选 例如: INSERT INTO table1 (t1_col1, t1_col2, t1_col3) SELECT t2.t1_col1, t2.t1_col2, t3.t1_col3, t4.filter_col FROM table2 t2 INNER JOIN table3 t3 ON t2.t1_col1 = t3.t1_col1 INNER JOIN table4 t4
INSERT INTO table1 (t1_col1, t1_col2, t1_col3)
SELECT
t2.t1_col1,
t2.t1_col2,
t3.t1_col3,
t4.filter_col
FROM table2 t2
INNER JOIN table3 t3 ON t2.t1_col1 = t3.t1_col1
INNER JOIN table4 t4 ON t4.filter_col = t2.filer_col
WHERE t4.filter_col = 'value';
表1只有列t1\u col1、t1\u col2和t1\u col3,因此,当我尝试运行此操作时,它会按预期失败:
ERROR: INSERT has more expressions than target columns
因此,我的问题是,我如何仍然包括过滤器,但指定SELECT语句中的哪些列应该在INSERT INTO语句中使用
非常感谢您的帮助 您必须在select中选择相同数量的列,因此从选择中删除
t4.filter\u col
INSERT INTO table1 (t1_col1, t1_col2, t1_col3)
SELECT
t2.t1_col1,
t2.t1_col2,
t3.t1_col3
FROM table2 t2
INNER JOIN table3 t3 ON t2.t1_col1 = t3.t1_col1
INNER JOIN table4 t4 ON t4.filter_col = t2.filer_col
WHERE t4.filter_col = 'value';
注意:您不必选择在联接或筛选中使用的所有列您必须选择相同数量的列,因此从选择中删除
t4.filter\u col
INSERT INTO table1 (t1_col1, t1_col2, t1_col3)
SELECT
t2.t1_col1,
t2.t1_col2,
t3.t1_col3
FROM table2 t2
INNER JOIN table3 t3 ON t2.t1_col1 = t3.t1_col1
INNER JOIN table4 t4 ON t4.filter_col = t2.filer_col
WHERE t4.filter_col = 'value';
注意:您不一定要选择在联接或筛选中使用的所有列这几乎是正确的,但您选择的列太多了。试试这个:
INSERT INTO table1 (t1_col1, t1_col2, t1_col3)
SELECT
t2.t1_col1,
t2.t1_col2,
t3.t1_col3
FROM table2 t2
INNER JOIN table3 t3 ON t2.t1_col1 = t3.t1_col1
INNER JOIN table4 t4 ON t4.filter_col = t2.filer_col
WHERE t4.filter_col = 'value';
几乎是对的,但您选择的列太多了。试试这个:
INSERT INTO table1 (t1_col1, t1_col2, t1_col3)
SELECT
t2.t1_col1,
t2.t1_col2,
t3.t1_col3
FROM table2 t2
INNER JOIN table3 t3 ON t2.t1_col1 = t3.t1_col1
INNER JOIN table4 t4 ON t4.filter_col = t2.filer_col
WHERE t4.filter_col = 'value';
只要不选择
t4.filter\u col
,您仍然可以在where
子句中使用它。您不需要在select列中放置t4.filter\u col
,就可以在查询中使用它。只要不选择t4.filter\u col
,您仍然可以在where
子句中使用它。您不需要在SELECT列中放置t4.filter\u col
,就可以在查询中使用它。