【问题标题】:Finding matching pairs between two tables and returning a third value to be inserted查找两个表之间的匹配对并返回要插入的第三个值
【发布时间】:2016-01-24 07:48:39
【问题描述】:

我正在使用 MySQL 认为 phpMyAdmin 的数据库工作,重要的是要知道我对 MySQL 的了解非常有限。

我的数据库中有两个表。我需要在第二个表(wp_posts_thumbs)中取img_id列的值,在第一个表(wp_posts)中找到匹配对,在第一个表的ID列中取对应的值,插入到第二个表中的post_parent 列。

这是第一个表 (wp_posts) 的样子:

+------+-------------------------+
| ID   | img_id                  |
+------+-------------------------+
| 1    | W048zewxemq1tw0810aiec  |
| 2    | W0481l2lv4npdczok5mmucl |
| 3    | W0481j9w7fg80b8gkiida85 |
+------+-------------------------+

这是第二个表 (wp_posts_thumbs) 的样子:

+------+-------------------------+-------------+
| ID   | img_id                  | post_parent |
+------+-------------------------+-------------+
| 101  | W048zewxemq1tw0810aiec  | 0           |
| 102  | W0481l2lv4npdczok5mmucl | 0           |
| 103  | W0481j9w7fg80b8gkiida85 | 0           |
+------+-------------------------+-------------+

提前谢谢各位:)

【问题讨论】:

  • 你不应该也发布你的第三张表(post_parent)吗?
  • 没有第三个表,伙计:/post_parent 是位于wp_posts_thumbs 表中的第三列,就像我在消息内容中说明的那样。
  • 抱歉误读了您的问题

标签: mysql phpmyadmin string-matching multiple-tables


【解决方案1】:
CREATE TABLE `wp_posts` (
  `Id` INT(11) PRIMARY KEY,
  `img_id` VARCHAR(100) UNIQUE NOT NULL
);
CREATE TABLE `wp_posts_thumbs` (
  `Id` INT(11) PRIMARY KEY,
  `img_id` VARCHAR(100) UNIQUE NOT NULL,
  `post_parent` INT(11)
);

INSERT INTO wp_posts (id,img_id) VALUES (1,'aiec');
INSERT INTO wp_posts (id,img_id) VALUES (2,'mucl');
INSERT INTO wp_posts (id,img_id) VALUES (3,'da85');
INSERT INTO wp_posts_thumbs (id,img_id,post_parent) VALUES (101,'aiec',0);
INSERT INTO wp_posts_thumbs (id,img_id,post_parent) VALUES (102,'mucl',0);
INSERT INTO wp_posts_thumbs (id,img_id,post_parent) VALUES (103,'da85',0);

UPDATE wp_posts_thumbs 
    LEFT JOIN wp_posts 
        ON wp_posts_thumbs.img_id = wp_posts.img_id 
    SET post_parent = wp_posts.id 
    WHERE wp_posts_thumbs.img_id = wp_posts.img_id; 

产生以下结果:

101 aiec    1
102 mucl    2
103 da85    3

【讨论】:

  • 我正在尝试测试您的建议,但收到以下错误消息:#1052 - Column 'post_parent' in field list is ambiguous
  • 这很奇怪,我没有收到这样的消息。不管怎样,试试wp_posts_thumbs.post_parent
  • 是的,我在发布之前的评论后自己设法做到了:P 但我已经等了 9 个小时,MySQL 还没有完成处理该查询。我可能需要添加新的索引或其他东西。太长了不知道怎么解决我只有 4500 行,应该不会花那么长时间。
  • 这也很奇怪。对于 4500 行,即使您没有指定任何索引,它也应该很快完成。但是,可以,尝试在两个表上为img_id 添加索引,并确保在更新post_parent没有 对其进行索引。 (如果需要索引,则在更新之前删除索引,并在更新完成后重新添加。)
  • 天哪!现在它在不到一分钟的时间内完美运行!断点是索引img_id 列。那个小东西加快了整个过程。我永远不会猜到索引有多重要。谢谢你,非常感谢你的帮助,@Mike Nakis!多亏了你,我学到了很多东西(也解决了一个小问题:P)。
【解决方案2】:

这需要 UPDATE 和 JOIN。你可以参考this previous question

SQL Fiddle

MySQL 5.6 架构设置

CREATE TABLE wp_posts
    (`ID` int, `img_id` varchar(23))
;

INSERT INTO wp_posts
    (`ID`, `img_id`)
VALUES
    (1, 'W048zewxemq1tw0810aiec'),
    (2, 'W0481l2lv4npdczok5mmucl'),
    (3, 'W0481j9w7fg80b8gkiida85')
;


CREATE TABLE wp_posts_thumbs
    (`ID` int, `img_id` varchar(23), `post_parent` int)
;

INSERT INTO wp_posts_thumbs
    (`ID`, `img_id`, `post_parent`)
VALUES
    (101, 'W048zewxemq1tw0810aiec', 0),
    (102, 'W0481l2lv4npdczok5mmucl', 0),
    (103, 'W0481j9w7fg80b8gkiida85', 0)
;

查询 1

UPDATE wp_posts_thumbs  wpt
   JOIN wp_posts wp ON wpt.img_id = wp.img_id  
   SET wpt.post_parent = wp.id
;

SELECT * FROM wp_posts_thumbs

Results

|  ID |                  img_id | post_parent |
|-----|-------------------------|-------------|
| 101 |  W048zewxemq1tw0810aiec |           1 |
| 102 | W0481l2lv4npdczok5mmucl |           2 |
| 103 | W0481j9w7fg80b8gkiida85 |           3 |

【讨论】:

    猜你喜欢
    • 2020-04-11
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多