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
Multiple updates in one SQL query including a select command and a provided variable
Old 08-24-2010, 03:29 AM Multiple updates in one SQL query including a select command and a provided variable
Experienced Talker

Posts: 37
Trades: 0
I'm looking to do the following query:

PHP Code:
INSERT INTO users field1field2field3 )  (SELECT field1field2field3 FROM tempusers WHERE email='some@email.com'
however, I also want to put a value in one field as set manually. I've tried many different ways and they do not work. This is one thing I've tried, which illustrates what I'm trying to accomplish:

PHP Code:
INSERT INTO users field1field2field3field4 )  (SELECT field1field2field3 FROM tempusers WHERE email='some@email.com'), 'field4data' 
but that did not work. I know you can use SELECT statements on the fly to retrieve data to insert into fields but can you do that alongside directly provided values as well in the same query, or is that something MySQL can't do?
catatung is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-24-2010, 04:33 AM Re: Multiple updates in one SQL query including a select command and a provided varia
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Of course you can. the second query is almost correct.
It should be
Code:
INSERT INTO users ( field1, field2, field3, field4 )  
SELECT field1, field2, field3,'field4data'
FROM tempusers WHERE email='some@email.com'
In the select, you can "add columns" by simply adding the value in the select list, that's what 'field4data' is.

And if you want to do that for several rows, you can use the UNION keyword
Code:
INSERT INTO users ( field1, field2, field3, field4 )  
SELECT field1, field2, field3,'field4data' FROM tempusers WHERE email='some@email.com'
UNION
SELECT field1, field2, field3,'field4data' FROM tempusers WHERE email='one@email.com'
UNION
SELECT field1, field2, field3,'field4data' FROM tempusers WHERE email='else@email.com'
__________________
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!
 
Reply     « Reply to Multiple updates in one SQL query including a select command and a provided variable
 

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.09884 seconds with 12 queries