Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Old 01-31-2009, 04:41 PM INSERT with SELECT
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Hi!

I've read some about this but I'm not sure weather or not I really understood it yet. I want to make a new insert, but one of the fields should be data selected from the same table.
I thought I could do this (data is irrelevant, just testing still)


INSERT INTO litters(litterNumber,father,mother,birthDate) VALUES((SELECT MAX(litterNumber) FROM litters),'46','47','2009-01-31')

Is this possible to do somehow?
If so, can I also select MAX(litterNumber)+1 since I want the new entry to have it's litterNumber increased by one?

Thanks
lizciz

EDIT: Oh, and if you want to see the error message, here it isYou can't specify target table 'litters' for update in FROM clause

Last edited by lizciz; 01-31-2009 at 04:43 PM.. Reason: Forgot error message
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
 
Register now for full access!
Old 01-31-2009, 04:57 PM Re: INSERT with SELECT
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
It works, but the query must be adapted to:
Code:
INSERT INTO litters(
  litterNumber,
  father,
  mother,
  birthDate
) 
SELECT
  MAX(litterNumber)+1
  ,46
  ,47
  ,'2009-01-31'
FROM litters
I know it's working in ms sql server, and postgresql. And I've just tested it in mysql 5.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 02-01-2009, 10:07 AM Re: INSERT with SELECT
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Indeed, it works great. Thank you!
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to INSERT with SELECT
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.11390 seconds with 12 queries