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 Where Not Exists

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-23-09, 18:51
nitrous nitrous is offline
Registered User
 
Join Date: Oct 2009
Posts: 2
Insert with Where Not Exists

hey guys I cant figure out what i need to change to make this work? version 5.0


Insert into stockchart (symbol,date,open,high,low,close,volume,adjclose) Values ('LVN.V','2009-10-22','0.26','0.37','0.25','0.37','809200','0.37') select symbol,date,open,high,low,close,volume,adjclose from dual WHERE NOT EXISTS (select * from stockchart where symbol='LVN.V' and date='2009-10-22' LIMIT 1);

Last edited by nitrous; 10-23-09 at 19:00.
Reply With Quote
  #2 (permalink)  
Old 10-23-09, 19:32
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
INSERT IGNORE INTO stockchart (symbol,date,open,high,low,close,volume,adjclose)
VALUES
('LVN.V','2009-10-22','0.26','0.37','0.25','0.37','809200','0.37')
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-26-09, 11:38
nitrous nitrous is offline
Registered User
 
Join Date: Oct 2009
Posts: 2
Quote:
Originally Posted by r937
INSERT IGNORE INTO stockchart (symbol,date,open,high,low,close,volume,adjclose)
VALUES
('LVN.V','2009-10-22','0.26','0.37','0.25','0.37','809200','0.37')
no unfortunately that will not work... none of those values can be the primary key becuase there will be a number of the same with the same symbol...

this is the table:

CREATE TABLE stockchart (
ID int NOT NULL AUTO_INCREMENT,
symbol VARCHAR(50),
date Date,
open DECIMAL(20,2),
high DECIMAL(20,2),
low DECIMAL(20,2),
close DECIMAL(20,2),
volume BIGINT,
adjclose DECIMAL(20,2),
LastUpdate timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY ( ID )
);

So it has to be done with the where not exists but i cant get it to work
Reply With Quote
  #4 (permalink)  
Old 10-26-09, 11:44
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
the key to the solution here is given by you in your query in the first post --

WHERE NOT EXISTS
(select * from stockchart
where symbol='LVN.V' and date='2009-10-22' LIMIT 1);


thus, the unique key should be on symbol and date

ALTER TABLE stockchart
ADD UNIQUE ( symbol,date )

now your INSERT IGNORE will work

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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