2013-04-11 34 views
0

我有一個網站的電子商務網站有一個價格表,看起來像這樣一個MySQL數據庫:INSERT INTO ....多行......在重複鍵

priceid (int, PK, AI) 
productid (int) 
membershipid (int) 
price (decimal(12,2)) 
quantity (int) 

這使得項目有不同程度的的定價要麼基於membershipidquantity(但這裏的價值將永遠是1)。

我需要與源表中更新此類似:

productid (int,PK) 
price1 (int) 
price2 (int) 
price3 (int) 

我不知道如何插入數據:

COLS:[productid, price, membershipid, quantity][priceid] 
ROWS:[productid, price1, 1, 1][n] 
ROWS:[productid, price2, 2, 1][n] 
ROWS:[productid, price3, 5, 1][n] 

N =新插入的ID,或PK更新,如果它已經存在)

我知道下面的代碼將無法正常工作,但基本上,我需要做的是這樣的:

INSERT INTO mysql.pricing 
    (prouductid, membershipid, quantity, price) 
SELECT productid, 1, 1, price1 from mysql.source 'row n 
SELECT productid, 2, 1, price2 from mysql.source 'row n+1 
SELECT productid, 5, 1, price3 from mysql.source 'row n+2 
ON DUPLICATE KEY UPDATE productid = VALUES(productid),membershipid = VALUES(membership), quantity = VALUES(quantity), price = VALUES(price); 

我需要三個不同的行,每行都有相同的[productid],但不同[priceid,membershipid,price]。


我已經搜索了答案,一直在桌子上撞了我的頭幾天。 任何想法?任何幫助將大大讚賞。

回答

0
You have mentioned priceid (int, PK, AI)- You can use on duplicate key update in  primary key but not on Auto Increment. 

There are two ways to solve this: 
Remove only Auto Increment from priceid and set the field as  (productid,membershipid,pricenumber) eg: 
Membershipid = 2 
productid = 33 
price1 = 11 
price2 = 12 
price3 = 13 
**priceid will be** 
33211 (price 1) 
33212 (price 2) 
33213 (price 3) 

This will always be unique...(for primary key), updating the following info. 
for memebershipid 2 , productid 33, price1 = 25, price2 = 28, price3 = 30 

insert will be as follows (priceid,membershipid,productid,price,quantity) 

replace into mysql.pricing values(33211,33,2,25,1);
replace into mysql.pricing values(33222,33,2,28,1);
replace into mysql.pricing values(33233,33,2,30,1);

if the price2 has been changed from 28 to 29 you insert statment will be like this. 

replace into mysql.pricing values(33222,33,2,29,1);

(using replace instead of insert for update incase of already existing primary key) 

or 

other way is add another column in you mysql.pricing table with datetime, and priceid  will be primarykey & autoincrement 

mysql table columns 
priceid (int, PK, AI),productid (int),membershipid (int) price(decimal(12,2)),quantity  (int),insert_date (datetime) 

Above insert can be inserted as following using now() function (which returns current  date& time) 

insert into mysql.pricing values(33,2,25,1,now());
insert into mysql.pricing values(33,2,28,1,now());
insert into mysql.pricing values(33,2,30,1,now());

updating price2 (it will always be insert) 

insert into mysql.pricing values(33,2,29,1,now());

make all fetch result based on max insert_date(datetime field) for respective  membershipid,price1/price2.. 
Hope this solves your problem. 
+0

我很抱歉,但它是我很難破譯你的第一個方法是什麼意思。刪除AI後,我會運行這3次,每個與不同的membershipID? :REPLACE INTO mysql.pricing(priceid,prouductid,membershipid,quantity,price)從mysql.source中選擇priceid,productid,1,priceA – luxdvie 2013-04-12 17:47:29