INSERT INTO ... SELECT带有更多SELECT列

时间:2018-10-09 09:05:49

标签: sql postgresql select sql-insert

谢谢您的时间。

我有一个INSERT INTO ... SELECT设置,但我也想使用多余的列(在我要插入的表中不存在)进行过滤

例如:

Foreground

表1 仅具有列 t1_col1 t1_col2 t1_col3 ,所以当我尝试运行此命令时,它将失败预期:

<VisualState x:Name="Disabled">

    <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="HeaderContentPresenter" Storyboard.TargetProperty="Foreground">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextControlHeaderForegroundDisabled}" />
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderElement" Storyboard.TargetProperty="Background">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextControlBackgroundDisabled}" />
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderElement" Storyboard.TargetProperty="BorderBrush">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextControlBorderBrushDisabled}" />
        </ObjectAnimationUsingKeyFrames>
        <!--<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentElement" Storyboard.TargetProperty="Foreground">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextControlForegroundDisabled}" />
        </ObjectAnimationUsingKeyFrames>-->
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PlaceholderTextContentPresenter" Storyboard.TargetProperty="Foreground">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextControlPlaceholderForegroundDisabled}" />
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

所以我的问题是,如何仍可以包括过滤器,但要指定INSERT INTO语句中应使用SELECT语句中的哪些列。

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您在select中必须具有相同的列数,因此从选择中删除t4.filter_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';

注意:您不必选择所有在联接或过滤器中使用的列

答案 1 :(得分:1)

这几乎是正确的,但是您选择了太多一列。试试这个:

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';
相关问题