[英]Proper syntax for multiple table update in MySQL 5.5
I'm trying to verify the correct syntax to use to carry out an multiple table update in MySQL 5.5. 我正在尝试验证用于在MySQL 5.5中执行多表更新的正确语法。 I have two tables, the first is structured like this:
我有两个表,第一个表的结构如下:
| *post_id* | lat | long |
where post_id is a unique identifier. 其中post_id是唯一标识符。 The second table is a Wordpress postmeta table:
第二个表是Wordpress postmeta表:
| *meta_id* | post_id | meta_key | meta_value |
where meta_id
is a unique identifier. 其中
meta_id
是唯一标识符。 Post_ID
s from the first table match the second table. 来自第一个表的
Post_ID
与第二个表匹配。 Some of the meta_keys
in the second table include list-lat
and list-long
. 第二个表中的一些
meta_keys
包括list-lat
和list-long
。
I wish to update the values of long
in the first table with the corresponding value from the second table where long
is 0.000000 and lat
is not 0.000000. 我希望用第二个表中的相应值更新第一个表中的
long
值,其中long
是0.000000而lat
不是0.000000。 The following select statement identifies 130 rows: 以下选择语句标识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'
This is as expected. 这是预期的。 If I try a simple UPDATE and simulate it, I once again affect 130 rows:
如果我尝试一个简单的UPDATE并模拟它,我将再次影响130行:
UPDATE bch_coords
SET bch_coords.long = 0.0
WHERE bch_coords.long = 0.000000 AND bch_coords.lat <> 0.000000
However, once I try to incorporate the second table, the query ceases to work. 但是,一旦我尝试合并第二个表,查询就会停止工作。 The following two queries both affect 0 rows.
以下两个查询都影响0行。 The second is of course the update I wish to carry out.
第二个当然是我希望进行的更新。
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
I have not used ANSI
joins because they caused a failure of the query whenever used in an UPDATE
for some reason (also a mystery at this point). 我没有使用过
ANSI
联接,因为它们由于某种原因(无论在什么时候还是个谜)在UPDATE
使用时都会导致查询失败。
Can anyone shed some light on exactly what I've done wrong here? 谁能确切说明我在这里做错了什么?
I've created an SQLFiddle markdown below: 我在下面创建了一个SQLFiddle降价:
MySQL 5.5 Schema Setup : 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')
;
So, I have a data set that looks like this... 所以,我有一个看起来像这样的数据集...
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 |
+---------+---------+-------------+---------------+
...and after the update, I want to have a data set that looks like this... ...并且在更新之后,我想要一个看起来像这样的数据集...
SELECT * FROM bch_coords;
+---------+------+------+
| post_id | lat | long |
+---------+------+------+
| 1 | 16 | 137 |
| 2 | 0 | 0 |
| 3 | -15 | 173 |
| 4 | 136 | 5.1 |
| 5 | -87 | 56 |
+---------+------+------+
try this: 尝试这个:
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
or this: 或这个:
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
Incidentally, and more interesting than your actual question, it's sometimes forgotten that, if carefully constructed, a view can be used to update its underlying tables. 顺便说一句,它比您的实际问题更有趣,但有时会忘记,如果精心构建视图,就可以使用它来更新其基础表。 Consider the following:
考虑以下:
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 |
+---------+---------+-------------+---------------+
This won't work for the NULL values in the view 这不适用于视图中的NULL值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.