繁体   English   中英

MySQL 5.5中多表更新的正确语法

[英]Proper syntax for multiple table update in MySQL 5.5

我正在尝试验证用于在MySQL 5.5中执行多表更新的正确语法。 我有两个表,第一个表的结构如下:

| *post_id* | lat | long |

其中post_id是唯一标识符。 第二个表是Wordpress postmeta表:

| *meta_id* | post_id | meta_key | meta_value |

其中meta_id是唯一标识符。 来自第一个表的Post_ID与第二个表匹配。 第二个表中的一些meta_keys包括list-latlist-long

我希望用第二个表中的相应值更新第一个表中的long值,其中long是0.000000而lat不是0.000000。 以下选择语句标识130行:

SELECT * FROM bch_postmeta
JOIN bch_coords ON bch_coords.post_id = bch_postmeta.post_id
WHERE bch_coords.long = 0.000000 
AND bch_coords.lat <> 0.000000 AND bch_postmeta.meta_key = 'list-long'

这是预期的。 如果我尝试一个简单的UPDATE并模拟它,我将再次影响130行:

UPDATE bch_coords
SET bch_coords.long = 0.0 
WHERE bch_coords.long = 0.000000 AND bch_coords.lat <> 0.000000

但是,一旦我尝试合并第二个表,查询就会停止工作。 以下两个查询都影响0行。 第二个当然是我希望进行的更新。

UPDATE bch_coords, bch_postmeta
SET bch_coords.long = 0.0 
WHERE bch_coords.long = 0.000000 AND bch_coords.lat <> 0.000000

UPDATE bch_coords, bch_postmeta
SET bch_coords.long = bch_postmeta.meta_value
WHERE bch_coords.post_id = bch_postmeta.post_id AND bch_postmeta.meta_key = 'list-long'
AND bch_coords.long = 0.000000 AND bch_coords.lat <> 0.000000

我没有使用过ANSI联接,因为它们由于某种原因(无论在什么时候还是个谜)在UPDATE使用时都会导致查询失败。

谁能确切说明我在这里做错了什么?

我在下面创建了一个SQLFiddle降价:

SQL小提琴

MySQL 5.5模式设置

CREATE TABLE bch_coords
    (`post_id` int, `lat` numeric, `long` numeric, UNIQUE(post_id))
;

INSERT INTO bch_coords
    (`post_id`, `lat`, `long`)
VALUES
    (1, 15.986132, 136.82515),
    (2, 0.000000, 0.000000),
    (3, -15.23850, 0.000000),
    (4, 136.32067, 0.000000),
    (5, -87.123567, 56.12396)
;


CREATE TABLE bch_postmeta
    (`meta_id` int, `post_id` int, `meta_key` varchar(13), `meta_value` varchar(15), UNIQUE(meta_id))
;

INSERT INTO bch_postmeta
    (`meta_id`, `post_id`, `meta_key`, `meta_value`)
VALUES
    (1, 1, '''list-lat''', '15.986132'),
    (2, 1, '''list-long''', '136.82515'),
    (3, 1, '''country''', '''Switzerland'''),
    (4, 1, '''state''', '''Valais'''),
    (5, 2, '''list-lat''', '0.000000'),
    (6, 2, '''list-long''', '0.000000'),
    (7, 2, '''country''', '''Australia'''),
    (8, 3, '''list-lat''', '-15.00'),
    (9, 3, '''list-long''', '173.62198'),
    (10, 3, '''country''', '''USA'''),
    (11, 4, '''list-lat''', '136.32067'),
    (12, 4, '''list-long''', '5.123997'),
    (13, 5, '''list-lat''', '-88.125'),
    (14, 5, '''list-long''', '56.12396')
;

所以,我有一个看起来像这样的数据集...

SELECT * FROM bch_coords;
+---------+------+------+
| post_id | lat  | long |
+---------+------+------+
|       1 |   16 |  137 |
|       2 |    0 |    0 |
|       3 |  -15 |    0 |
|       4 |  136 |    0 |
|       5 |  -87 |   56 |
+---------+------+------+

SELECT * FROM bch_postmeta;
+---------+---------+-------------+---------------+
| meta_id | post_id | meta_key    | meta_value    |
+---------+---------+-------------+---------------+
|       1 |       1 | 'list-lat'  | 15.986132     |
|       2 |       1 | 'list-long' | 136.82515     |
|       3 |       1 | 'country'   | 'Switzerland' |
|       4 |       1 | 'state'     | 'Valais'      |
|       5 |       2 | 'list-lat'  | 0.000000      |
|       6 |       2 | 'list-long' | 0.000000      |
|       7 |       2 | 'country'   | 'Australia'   |
|       8 |       3 | 'list-lat'  | -15.00        |
|       9 |       3 | 'list-long' | 173.62198     |
|      10 |       3 | 'country'   | 'USA'         |
|      11 |       4 | 'list-lat'  | 136.32067     |
|      12 |       4 | 'list-long' | 5.123997      |
|      13 |       5 | 'list-lat'  | -88.125       |
|      14 |       5 | 'list-long' | 56.12396      |
+---------+---------+-------------+---------------+

...并且在更新之后,我想要一个看起来像这样的数据集...

SELECT * FROM bch_coords;
+---------+------+------+
| post_id | lat  | long |
+---------+------+------+
|       1 |   16 |  137 |
|       2 |    0 |    0 |
|       3 |  -15 |  173 |
|       4 |  136 |  5.1 |
|       5 |  -87 |   56 |
+---------+------+------+

尝试这个:

UPDATE bch_coords
SET bch_coords.long = (SELECT bch_postmeta.meta_value 
                       FROM bch_postmeta 
                       WHERE bch_postmeta.post_id = bch_coords.post_id
                       AND bch_postmeta.meta_key = 'list-long'
                       LIMIT 1)
WHERE bch_coords.long = 0.000000 
AND bch_coords.lat <> 0.000000

或这个:

UPDATE bch_coords 
INNER JOIN bch_postmeta ON (bch_coords.post_id = bch_postmeta.post_id AND bch_postmeta.meta_key = 'list-long')
SET bch_coords.long = bch_postmeta.meta_value
WHERE bch_coords.long = 0.000000 
AND bch_coords.lat <> 0.000000 

顺便说一句,它比您的实际问题更有趣,但有时会忘记,如果精心构建视图,就可以使用它来更新其基础表。 考虑以下:

SELECT * FROM bch_postmeta;
+---------+---------+-------------+---------------+
| meta_id | post_id | meta_key    | meta_value    |
+---------+---------+-------------+---------------+
|       1 |       1 | 'list-lat'  | 15.986132     |
|       2 |       1 | 'list-long' | 136.82515     |
|       3 |       1 | 'country'   | 'Switzerland' |
|       4 |       1 | 'state'     | 'Valais'      |
|       5 |       2 | 'list-lat'  | 0.000000      |
|       6 |       2 | 'list-long' | 0.000000      |
|       7 |       2 | 'country'   | 'Australia'   |
|       8 |       3 | 'list-lat'  | -15.00        |
|       9 |       3 | 'list-long' | 173.62198     |
|      10 |       3 | 'country'   | 'USA'         |
|      11 |       4 | 'list-lat'  | 136.32067     |
|      12 |       4 | 'list-long' | 5.123997      |
|      13 |       5 | 'list-lat'  | -88.125       |
|      14 |       5 | 'list-long' | 56.12396      |
+---------+---------+-------------+---------------+

CREATE VIEW v_bch_postmeta AS
SELECT a.post_id
     , a.meta_value list_lat
     , b.meta_value list_long
     , c.meta_value country
     , d.meta_value state
  FROM bch_postmeta a
  LEFT
  JOIN bch_postmeta b
    ON b.post_id = a.post_id
   AND b.meta_key = "'list-long'"
  LEFT
  JOIN bch_postmeta c
    ON c.post_id = a.post_id
   AND c.meta_key = "'country'"
  LEFT
  JOIN bch_postmeta d
    ON d.post_id = a.post_id
   AND d.meta_key = "'state'"
 WHERE a.meta_key = "'list-lat'";

Query OK, 0 rows affected (0.01 sec)

SELECT * FROM v_bch_postmeta;
+---------+-----------+-----------+---------------+----------+
| post_id | list_lat  | list_long | country       | state    |
+---------+-----------+-----------+---------------+----------+
|       1 | 15.986132 | 136.82515 | 'Switzerland' | 'Valais' |
|       2 | 0.000000  | 0.000000  | 'Australia'   | NULL     |
|       3 | -15.00    | 173.62198 | 'USA'         | NULL     |
|       4 | 136.32067 | 5.123997  | NULL          | NULL     |
|       5 | -88.125   | 56.12396  | NULL          | NULL     |
+---------+-----------+-----------+---------------+----------+

UPDATE v_bch_postmeta SET list_lat = 1.0 WHERE post_id = 2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

SELECT * FROM bch_postmeta;
+---------+---------+-------------+---------------+
| meta_id | post_id | meta_key    | meta_value    |
+---------+---------+-------------+---------------+
|       1 |       1 | 'list-lat'  | 15.986132     |
|       2 |       1 | 'list-long' | 136.82515     |
|       3 |       1 | 'country'   | 'Switzerland' |
|       4 |       1 | 'state'     | 'Valais'      |
|       5 |       2 | 'list-lat'  | 1.0           |
|       6 |       2 | 'list-long' | 0.000000      |
|       7 |       2 | 'country'   | 'Australia'   |
|       8 |       3 | 'list-lat'  | -15.00        |
|       9 |       3 | 'list-long' | 173.62198     |
|      10 |       3 | 'country'   | 'USA'         |
|      11 |       4 | 'list-lat'  | 136.32067     |
|      12 |       4 | 'list-long' | 5.123997      |
|      13 |       5 | 'list-lat'  | -88.125       |
|      14 |       5 | 'list-long' | 56.12396      |
+---------+---------+-------------+---------------+

这不适用于视图中的NULL值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM