If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > insert with subselect question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-14-06, 16:51
Thrakazog Thrakazog is offline
Registered User
 
Join Date: Jul 2006
Posts: 4
Question insert with subselect question

Hello!

I've got two tables:

users (id, username, password, entity_type_id, user_fullname_id)
user_fullnames (id, f_name, m_name, l_name)

I'm trying to select all the ids from user_fullnames that don't have a corresponding user_fullname_id in the users table, then insert them(along with an entity_type_id of 2 for each one) into the users table.

INSERT INTO users (entity_type_id, user_fullname_id)
VALUES (2, (SELECT user_fullnames.id FROM user_fullnames
WHERE NOT EXISTS (SELECT * FROM users WHERE user_fullnames.id = user_fullname_id)));

The subselect works, and gives the expected results. But when I use it with the insert, I get "You can't specify target table 'users' for update in FROM clause". If I copy users into a temp table first(or do it with a join instead), I get "Subquery returns more than 1 row". Anyone see what I'm doing wrong?

Thanks in advance
~Thrakazog
Reply With Quote
  #2 (permalink)  
Old 07-15-06, 09:25
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
could you please show the query where you are able to successfully store the rows you want into a temp table
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 07-17-06, 12:47
Thrakazog Thrakazog is offline
Registered User
 
Join Date: Jul 2006
Posts: 4
Question

> could you please show the query where you are able to successfully
> store the rows you want into a temp table

All I'm doing here is copying users into a temp table, to try and get around the "You can't specify target table 'users' for update in FROM clause" error.

CREATE TEMPORARY TABLE t_users
SELECT * FROM users;
INSERT INTO users (entity_type_id, user_fullname_id)
VALUES (2, (SELECT user_fullnames.id FROM user_fullnames
WHERE NOT EXISTS (SELECT * FROM t_users
WHERE user_fullnames.id = t_users.user_fullname_id)));

thanks,
~Thrakazog
Reply With Quote
  #4 (permalink)  
Old 07-17-06, 17:54
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
try it like this --
Code:
create temporary table newusers
select user_fullnames.id 
  from user_fullnames
left outer
  join t_users
    on t_users.user_fullname_id = user_fullnames.id
 where t_users.user_fullname_id is null
then you can insert newusers into users
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 07-18-06, 12:30
Thrakazog Thrakazog is offline
Registered User
 
Join Date: Jul 2006
Posts: 4
Question

Close! This give me the data that I need(one column), but then I need to get it into users.user_fullname_id, along with a static value of 2 into users.entity_type_id for each row. This is the part where I get the "subquery returns more than 1 row" error. I could do it in two separate steps, except both user_fullname_id and entity_type_id are non-null.

thanks!
~Thrakazog
Reply With Quote
  #6 (permalink)  
Old 07-18-06, 12:55
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
okay, just take that concept and run with it --

the table newusers should have the exact same data layout as the users table, and you fill it up with your CREATE/SELECT statement there

your second step would then be to insert the contents of newusers back into users with an INSERT/SELECT statement, which, since the layouts are identical, will not require mentioning columns --
Code:
insert
  into users
select *
  from newusers
one of the very rare instances when it is acceptable to use the dreaded, evil "select star"
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 07-19-06, 18:01
Thrakazog Thrakazog is offline
Registered User
 
Join Date: Jul 2006
Posts: 4
Thank you! Y'all are greatly appreciated....

~T
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On