2
這似乎重構在我的頭上可行的,但我想有人來檢查我的邏輯:對於新項目和編輯項目都使用REPLACE INTO?
當前進程:
if (item == new) {
INSERT INTO basic_table
$itemUID = get last insert ID // item's UID
INSERT INTO another_table // more stuff
INSERT INTO another_table2 // more stuff
INSERT INTO another_table3 // more stuff
} else {
$itemUID = $_POST['uid']
UPDATE basic_table
REPLACE INTO another_table // more stuff
REPLACE INTO another_table2 // more stuff
REPLACE INTO another_table3 // more stuff
}
(注 - REPLACE INTO用於現有項目,因爲他們根據它們的初始配置,可能有也可能沒有所有表中的條目)
我想到,由於所有後續查詢都是相同的,除了INSERT INTO // REPLACE INTO
,我應該能夠重構爲:
if (item == new) {
INSERT INTO basic_table
$itemUID = get last insert ID // item's UID
} else {
$itemUID = $_POST['uid']
UPDATE basic_table
}
REPLACE INTO another_table // more stuff
REPLACE INTO another_table2 // more stuff
REPLACE INTO another_table3 // more stuff
考慮到我使用的是PDO而且每個查詢有很多的參數,這將節省crapload的空間。
但我想先把它發佈在這裏,以確保我不會忽略某些東西。
這個重構會產生相同的結果嗎?
你爲什麼不對所有四個人使用'REPLACE'? –
看看INSERT..ON DUPLICATE KEY UPDATE語句。 – Devart