Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

More than one row returned by subquery

Status
Not open for further replies.

abenstex

Programmer
Jan 9, 2005
47
DE
Hi,

i have a table where i want to store which id occurs in which table, so the table looks basically like this:
table_name id
t1 1
t1 2
...
t2 150
t2 151

I have been trying to come up with a query that inserst the values accordingly but somehow i can't seem to get it right:
Code:
insert into table_overview(table_name, id)
select 't1', (select distinct id from t1) from t1;
Where the above mentioned error occurs. Then i tried a query like:
Code:
insert into table_overview(table_name, id)
select 't1', id where id in(select distinct id from t1) from t1;
But that didn't want to work either. So, i am really happy about any advice...
 
Hi

Your second try is better, you just reversed the clause's order. The [tt]from[/tt] should occure before the [tt]where[/tt].
Code:
[b]insert into[/b] table_overview(table_name, id)
[b]select[/b] [i]'t1'[/i], id [b]from[/b] t1 [b]where[/b] id [b]in[/b]([b]select distinct[/b] id [b]from[/b] t1);
But that [tt]select[/tt] still lacks some logic. At least I do not understand the meaning of using the sub-select.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top