使用子查询中的值进行复杂的MySQL更新

时间:2013-12-29 17:33:47

标签: mysql sql ruby-on-rails

开始时的圣诞快乐!

我关注MySQL表:

memo_words
+----+-----------------------+---------+
| id | current_definition_id | word_id |
+----+-----------------------+---------+
|  1 |                     1 |       1 |
|  2 |                     2 |       1 |
|  3 |                     1 |       2 |
|  4 |                     2 |       2 |
+----+-----------------------+---------+

words
+----+
| id |
+----+
|  1 |
|  2 |
+----+

definitions
+----+
| id |
+----+
|  1 |
|  2 |
+----+

definitions_words
+---------+---------------+----------+
| word_id | definition_id | position | 
+---------+---------------+----------+
|       1 |             1 |        0 |
|       1 |             2 |        0 |
|       2 |             1 |        0 |
|       2 |             2 |        0 |
+---------+---------------+----------+

我希望查询返回的任何行与更新相关(definition_id,word_id)definitions_words.position以及别名position

SELECT word_id, current_definition_id as definition_id, COUNT(*) as position
FROM memo_words
WHERE current_definition_id IS NOT NULL
GROUP BY current_definition_id, word_id;

+---------+---------------+----------+
| word_id | definition_id | position |
+---------+---------------+----------+
|       1 |             1 |        1 |
|       2 |             1 |        1 |
|       1 |             2 |        1 |
|       2 |             2 |        1 |
+---------+---------------+----------+

一次查询。

所以结果此操作应如下所示:

definitions_words
+---------+---------------+----------+
| word_id | definition_id | position | 
+---------+---------------+----------+
|       1 |             1 |        1 |
|       1 |             2 |        1 |
|       2 |             1 |        1 |
|       2 |             2 |        1 |
+---------+---------------+----------+

DDL:

CREATE TABLE memo_words( id int, current_definition_id int, word_id int, PRIMARY KEY(id));
INSERT INTO memo_words VALUES (1, 1, 1), (2, 2, 1), (3, 1, 2), (4, 2, 2);

CREATE TABLE words(id int, PRIMARY KEY(id));
INSERT INTO words VALUES(1), (2);


CREATE TABLE definitions(id int, PRIMARY KEY(id));
INSERT INTO definitions VALUES(1), (2);

CREATE TABLE definitions_words(word_id int, definition_id int , position int);
INSERT INTO definitions_words VALUES(1,1,0), (1,2,0), (2,1,0), (2,2,0);

2 个答案:

答案 0 :(得分:1)

UPDATE definitions_words d
JOIN (SELECT word_id, current_definition_id, COUNT(*) as position
      FROM memo_words
      WHERE current_definition_id IS NOT NULL
      GROUP BY current_definition_id, word_id) m
ON d.word_id = m.word_id AND d.definition_id = m.current_definition_id
SET d.position = m.position

SQLFIDDLE

答案 1 :(得分:1)

我会使用join和子查询来执行此操作:

update definitions_words dw join
       (SELECT word_id, current_definition_id as definition_id, COUNT(*) as position
        FROM memo_words
        WHERE current_definition_id IS NOT NULL
        GROUP BY current_definition_id, word_id
       ) mw
       on dw.word_id = mw.word_id and dw.definition_id = mw.definition_id
    set dw.position = mw.position;