简体   繁体   English

在SQL中获取子查询错误

[英]getting subquery error in SQL

Trying to insert into some table using my current table . 尝试使用当前表插入某些表。 Here is the structure of my current Temptable: 这是我当前的Temptable的结构:

CustomerID  Name        Values      FakeName
1           John        10apples    10apples_20oranges_30bananas
1           John        20oranges   10apples_20oranges_30bananas
1           John        30bananas   10apples_20oranges_30bananas
2           Steve       15apples    15apples_25oranges_35bananas
2           Steve       25oranges   15apples_25oranges_35bananas
2           Steve       35bananas   15apples_25oranges_35bananas
3           Harvey      10apples    10apples_20oranges_30bananas
3           Harvey      20oranges   10apples_20oranges_30bananas
3           Harvey      30bananas   10apples_20oranges_30bananas

This is my peice of code that I am executing : 这是我正在执行的代码:

Insert into customer (FakeName,type,address)
select (select distinct FakeName from Temptable),
        2,
        xyz

 from customer c
 where c.fakename not in (select distinct Fakename from TempTable)

getting following error 得到以下error

Subquery returned more than 1 value. 子查询返回的值超过1。 This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression 当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做

I want to insert distinct Fakenames from temptable to customer table , making sure if the fake name already exist then not to insert a duplicate fakename 我想从temptable表到customer表中插入不同的假名,确保假名已经存在,然后不插入重复的假名。

select distinct FakeName from Temptable is not a scalar, so you can't use it like that. select distinct FakeName from Temptable不是标量,因此不能那样使用。

I think this is what you're after: 我认为这是您追求的目标:

Insert into customer (FakeName,type,address)
select distinct 
        FakeName,
        2,
        xyz
 from Temptable c
 where c.fakename not in (select distinct Fakename from customer)

I think you want something like this: 我想你想要这样的东西:

Insert into customer (FakeName, type, address)
    select distinct tt.FakeName, 2, 'xyz'
    from temptable tt
    where not exists (select 1 from customer c where c.fakename = tt.fakename);

Notes: 笔记:

  • xyz is undefined in your query (unless it is a column in customer, which seems unlikely). xyz在查询中未定义(除非它是customer中的一列,这似乎不太可能)。
  • not in will filter all rows if any name the subquery is NULL . 如果子查询的名称为NULLnot in将过滤所有行。 I replaced it with not exists . 我用not exists替换它。
  • I am speculating that you want to avoid duplicate entries in customer , so I changed the subquery. 我推测您要避免在customer重复输入,因此我更改了子查询。
  • When using in / not in with a subquery, select distinct is redundant. in子查询中使用in / not in时, select distinct是多余的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM