我有一個表來計算字出現次數,WORD_COUNT(WORD, TOTAL)
,它在另一個表上的觸發器中更新。這是在hierachichal查詢做過這樣的:字的Oracle複雜查詢:INSERT + SELECT CONNECT BY + ON DUPLICATE KEY
創建和總= 1:
INSERT INTO WORD_COUNT(TOTAL, WORD)
SELECT 1, TRANSLATE(regexp_substr(:NEW.TWEET, '[^[:punct:]|[:space:]]+', 1, level),
'áéíóúÁÉÍÓÚ',
'aeiouAEIOU')
FROM DUAL
WHERE LENGTH(regexp_substr(:NEW.TWEET, '[^[:punct:]|[:space:]]+', 1, level)) >= 5
CONNECT BY regexp_substr(:NEW.TWEET, '[^[:punct:]|[:space:]]+', 1, level) IS NOT NULL;
這項工作完美的,它分裂一個短語(鳴叫)進言,將它們插入WORD_COUNT。
現在的問題是,當我想增加重複鍵上的總數(WORD是主鍵)時,我需要添加一個ON DUPLICATE KEY
子句,該子句看起來與CONNECT BY
子句似乎不太一樣。
此查詢不編譯:
INSERT INTO WORD_COUNT(TOTAL, WORD)
SELECT 1, TRANSLATE(regexp_substr(:NEW.TWEET, '[^[:punct:]|[:space:]]+', 1, level),
'áéíóúÁÉÍÓÚ',
'aeiouAEIOU')
FROM DUAL
WHERE LENGTH(regexp_substr(:NEW.TWEET, '[^[:punct:]|[:space:]]+', 1, level)) >= 5
CONNECT BY regexp_substr(:NEW.TWEET, '[^[:punct:]|[:space:]]+', 1, level) IS NOT NULL
ON DUPLICATE KEY UPDATE TOTAL=TOTAL+1;
而這一次也不:
INSERT INTO WORD_COUNT(TOTAL, WORD)
WITH WORDS(WORD) AS
(SELECT DISTINCT 1,
TRANSLATE(regexp_substr(:NEW.TWEET, '[^[:punct:]|[:space:]]+', 1, level),
'áéíóúÁÉÍÓÚ',
'aeiouAEIOU')
FROM DUAL
WHERE LENGTH(regexp_substr(:NEW.TWEET, '[^[:punct:]|[:space:]]+', 1, level)) >= 5
CONNECT BY regexp_substr(:NEW.TWEET, '[^[:punct:]|[:space:]]+', 1, level) IS NOT NULL)
SELECT WORD FROM WORD_COUNT
ON DUPLICATE KEY UPDATE TOTAL = TOTAL + 1;
由於這種情況正在發生一個高流量表的觸發器中,我想在解決這一單一的查詢,但也許是時候想一箇中間表,是嗎?
謝謝
也許它加入最近:http://docs.oracle.com/cd/E17952_01/refman-5.0-en/insert-on-duplicate.html –
不幸不,鏈接關係MySQL。在鏈接中上升一級並收到「MySQL 5.0參考手冊」:http://docs.oracle.com/cd/E17952_01/refman-5.0-zh/ –