[英]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
. NULL
则not in
将过滤所有行。 I replaced it with not exists
. not exists
替换它。 customer
, so I changed the subquery. customer
重复输入,因此我更改了子查询。 in
/ not in
with a subquery, select distinct
is redundant. in
子查询中使用in
/ not in
时, select distinct
是多余的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.