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 Into [Table] Select ... question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-15-05, 06:26
divined divined is offline
Registered User
 
Join Date: Apr 2004
Posts: 110
Insert Into [Table] Select ... question (Solved)

Hello everybody

I`d like insert some fields from a table into another. The problem is that I`d also like some other fields to remain constant throughout each inserted record. Something like this :

Insert Into Table Values (Null, Select Field From Table1 Where [clause],1,1);

Is this a valid MySQL statement and if not can I do something else to the get the result above?

Last edited by divined; 11-16-05 at 06:59.
Reply With Quote
  #2 (permalink)  
Old 11-15-05, 07:20
Nura Nura is offline
Registered User
 
Join Date: Oct 2005
Posts: 21
Insert

Hi,
You could give the name of the fields to be insert so that other fields remain unchanged.
Ex:
INSERT INTO Table(field1,field2) VALUES('', '');

field1 and field2 are the name of the field in table to be inserted.Now if one filed is to be constant the give the field name and the constant value.
I think it solves your problem if i have understood you correctly.
Reply With Quote
  #3 (permalink)  
Old 11-15-05, 08:46
felixg felixg is offline
Registered User
 
Join Date: Apr 2005
Location: Lier, Belgium
Posts: 122
Quote:
Originally Posted by divined
Insert Into Table Values (Null, Select Field From Table1 Where [clause],1,1);
Code:
INSERT INTO Table
SELECT
    NULL,
    Field,
    1,
    1
FROM Table1
WHERE [clause];
--
felix
Reply With Quote
  #4 (permalink)  
Old 11-15-05, 11:29
divined divined is offline
Registered User
 
Join Date: Apr 2004
Posts: 110
cool, I`ll try it first thing in the morning.
Reply With Quote
  #5 (permalink)  
Old 11-16-05, 02:05
divined divined is offline
Registered User
 
Join Date: Apr 2004
Posts: 110
One further thing. In the SQL statement below :

Code:
INSERT INTO Table
SELECT
    NULL,
    Field,
    1,
    1
FROM Table1
WHERE [clause];
can I also tell MySQL that I only want the Insert to proceed if Field does not already exist in Table. Otherwise only the offending record should be discarded and MySQL should proceed to Insert the next record in Table1.

Can this be done with one statement?
Reply With Quote
  #6 (permalink)  
Old 11-16-05, 02:56
felixg felixg is offline
Registered User
 
Join Date: Apr 2005
Location: Lier, Belgium
Posts: 122
Quote:
Originally Posted by divined
can I also tell MySQL that I only want the Insert to proceed if Field does not already exist in Table. Otherwise only the offending record should be discarded and MySQL should proceed to Insert the next record in Table1.
Put a UNIQUE index on Field and use INSERT IGNORE INTO ...

--
felix
Reply With Quote
  #7 (permalink)  
Old 11-16-05, 03:53
divined divined is offline
Registered User
 
Join Date: Apr 2004
Posts: 110
You mean that I should cerate the table as this :

Code:
CREATE TABLE EGGYHSH(Kwd_Eggyhshs INTEGER UNSIGNED AUTO_INCREMENT,
		                 Kwd_Mhxanhmatos VARCHAR(7),
	 	                 Liksi_Eggyhshs BOOL,
                   	         Eidopoihsh BOOL,
		                 INDEX Kwd_Mhxanhmatos_ind (Kwd_Mhxanhmatos) UNIQUE,
		                 FOREIGN KEY(Kwd_Mhxanhmatos REFERENCES            MHXANHMA(Kwd_Mhxanhmatos) ON DELETE SET CASCADE),
		                PRIMARY KEY(Kwd_Eggyhshs)
		     ) TYPE = INNODB;
Is this syntactically correct?
Reply With Quote
  #8 (permalink)  
Old 11-16-05, 04:03
felixg felixg is offline
Registered User
 
Join Date: Apr 2005
Location: Lier, Belgium
Posts: 122
Quote:
Originally Posted by divined
Is this syntactically correct?
The CREATE TABLE syntax is explained here: http://dev.mysql.com/doc/refman/4.1/...ate-table.html

Code:
... UNIQUE  Kwd_Mhxanhmatos_ind (Kwd_Mhxanhmatos) ...
will probably work.

--
felix
Reply With Quote
  #9 (permalink)  
Old 11-16-05, 06:56
divined divined is offline
Registered User
 
Join Date: Apr 2004
Posts: 110
This is how I finally cerated the table :

Code:
CREATE TABLE Eggyhsh(Kwd_Eggyhshs INTEGER UNSIGNED AUTO_INCREMENT,
		     Kwd_Mhxanhmatos VARCHAR(7),
		     Liksi_Eggyhshs BOOL,
		     Eidopoihsh BOOL,
		     UNIQUE INDEX Kwd_Mhxanhmatos_ind (Kwd_Mhxanhmatos),
		     FOREIGN KEY(Kwd_Mhxanhmatos) REFERENCES Mhxanhma(Kwd_Mhxanhmatos) ON DELETE CASCADE,
		     PRIMARY KEY(Kwd_Eggyhshs)) TYPE = INNODB;
thx, to everyone that helped!!
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