Sponsored by NuSphere - PHP Software for PHP Application Developers - On Sale This Week for $100



Go Back   PHP-Editors > Linux, Apache, MySQL > MySQL Help

MySQL Help Post any question relating to MySQL here and hopefully someone can help

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 2007-09-20, 11:06 AM
Junior Member
 
Join Date: Sep 2007
Posts: 2
faif is on a distinguished road
Default insert with nested select problem

I want to implement a query similar to this in mysql:

Code:
insert into table1 (field1)
    (select t2.field2
    from table2 t2
     where t2.field3="string" and t2.field4=table1.field4)
the reference to table1 from the inner query (table1.field4) does not work.

Is there a way to achieve this?
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 2007-09-21, 05:23 AM
Xnuiem's Avatar
Senior Member
 
Join Date: May 2004
Location: DFW, Texas
Posts: 1,104
Xnuiem will become famous soon enough
Send a message via Yahoo to Xnuiem
Default

doing it like that creates a circular reference and it cant logically complete since it cant compare something that doesnt exist yet.

Remove the second argument from the where clause and it will work fine.
__________________
I rarely give code examples.
No, I have never used IIS or Windows of any kind as a web server. Get a real OS!
Please don't PM me, I won't respond.
Reply With Quote
  #3 (permalink)  
Old 2007-09-21, 09:46 AM
Junior Member
 
Join Date: Sep 2007
Posts: 2
faif is on a distinguished road
Default

Quote:
Originally Posted by Xnuiem View Post
doing it like that creates a circular reference and it cant logically complete since it cant compare something that doesnt exist yet.

Remove the second argument from the where clause and it will work fine.
Thank you for the answer, but unfortunately I've already tried this and it is not what I'm looking for.

I want to copy the values of the fields of table2 to the fields of table1. I use the t2.field4=table1.field4 because I want to copy the contents in the appropriate records, and don't create new. When I'm using a copy of table1 in the inner query, new records are created in table1, and this doesn't solve my problem.
Reply With Quote
  #4 (permalink)  
Old 2007-09-21, 11:12 AM
Xnuiem's Avatar
Senior Member
 
Join Date: May 2004
Location: DFW, Texas
Posts: 1,104
Xnuiem will become famous soon enough
Send a message via Yahoo to Xnuiem
Default

You will need to employ a scripting language then. This is the type of thing that spawned PL/SQL in Oracle and the like.

You can use PHP to do it.

The reason SQL doesn't allow that, is due to the rules of data normalization. Replicating the same data in multiple tables is inefficient, and hence SQL as a language doesn't really allow you to get straight to it, you need to go around.

There are reasons to do what you are talking about, especially in a data warehousing environment, but normally in a production database, replication should be kept to a minimum.
__________________
I rarely give code examples.
No, I have never used IIS or Windows of any kind as a web server. Get a real OS!
Please don't PM me, I won't respond.
Reply With Quote
Must read Review for Serious PHP Developers


NuSphere PhpED 5.0 : The Staff of php-editors.com recently spent a few days working with NuSphere PhpED 5.0 (a popular PHP IDE) and NuCoder 1.4 (a PHP Encoding Utility), read up on all the details.

Sponsored Links
Reply

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



All times are GMT +1. The time now is 05:11 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0
© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.