简体   繁体   English

如何使用来自另一个表的随机记录更新给定的mySQL表

[英]How to UPDATE a given mySQL table with random record from another table

I have a table tbl_random like this with about 3000 keywords and model_ids. 我有一个这样的表tbl_random其中包含约3000个关键字和model_ids。 Keyword are unique, model_ids are not: 关键字是唯一的,model_ids不是:

+---------+------------+---------+---------+--------------+
| keyword | model_id   | make    |  model  |  more_data   |
+---------+------------+---------+---------+--------------+
| apple1  | 15         |         |         |              |
| apple2  | 15         |         |         |              |
| pear    | 205        |         |         |              |
| cherry  | 307        |         |         |              |
| melon   | 5023       |         |         |              |
+---------+------------+---------+---------+--------------+

and I have a second table tbl_products with about 500K records that contains the actual products and details about them: 我还有第二个表tbl_products ,其中包含大约50万条记录,其中包含实际产品及其详细信息:

+---------+--------+------------+
| make    | model  | more_data  |
+---------+--------+------------+
| app1    | 15     | data1      |
| app1    | 15     | data2      |
| cher74  | 307    | data4      |
| melo2   | 5023   | data5      |
| pear53  | 205    | data3      |
+---------+--------+------------+

The common identifier between the two tables is model_id and model respectively. 这两个表之间的公共标识符分别是model_idmodel

What I need to do is to write an UPDATE MySQL query, which will update make model and more_data in tbl_random according to those criteria: 我需要做的是编写一个UPDATE MySQL查询, tbl_random根据这些条件更新more_data中的make modelmore_data

The query has to select one random row from tbl_products that matches every model_id from tbl_random for the unique keywords. 查询必须从选择一个随机行tbl_products ,每相匹配model_idtbl_random为独特的关键字。

I was trying to do this in various ways using UPDATE, LEFT JOIN and SELECT STATEMENTS but cannot get it to work so far. 我试图使用UPDATE,LEFT JOIN和SELECT STATEMENTS以各种方式执行此操作,但到目前为止无法正常工作。

My latest statement, trying to update only make is this one, however it does not work - MySQL starts executing it and nothing happens except that I need to restart MySQL to get it available again: 我最新的声明仅尝试更新make ,但这是行不通的-MySQL开始执行它,除了我需要重新启动MySQL以使其再次可用之外,什么也没有发生:

UPDATE tbl_random
        SET tbl_random.make =
        (SELECT tbl_products.make FROM tbl_products                     
        WHERE tbl_random.model_id = tbl_products.model
        GROUP BY tbl_random.keyword       
        ORDER BY RAND())

The final desired output for tbl_random is this one: tbl_random的最终所需输出是以下内容:

+---------+------------+---------+---------+--------------+
| keyword | model_id   | make    |  model  |  more_data   |
+---------+------------+---------+---------+--------------+
| apple1  | 15         | app1    |  15     |  data1       |
| apple2  | 15         | app1    |  15     |  data2       |
| pear    | 205        | pear53  |  205    |  data3       |
| cherry  | 307        | cher74  |  307    |  data4       |
| melon   | 5023       | melo2   |  5023   |  data5       |
+---------+------------+---------+---------+--------------+

Any help or suggestions will be appreciated! 任何帮助或建议,将不胜感激!

Looks like your inner select returns more than one item and then you are trying to assign it to a single field. 看起来您的内部选择返回了多个项目,然后您尝试将其分配给单个字段。

Try adding LIMIT 1 to your inner select. 尝试将LIMIT 1添加到内部选择中。

UPDATE tbl_random
    SET tbl_random.make =
    (SELECT tbl_products.make FROM tbl_products                     
    WHERE tbl_random.model_id = tbl_products.model
    GROUP BY tbl_random.keyword       
    ORDER BY RAND() LIMIT 1)

I think this might work if the firs option doesn't 我认为如果firs选项不起作用,这可能会起作用

REPLACE tbl_random (keyword, model_id, make, model) INTO
    SELECT rand2.keyword, rand2.model_id, (SELECT tbl_products.make FROM tbl_products                     
        WHERE rand2.model_id = tbl_products.model
        ORDER BY RAND() LIMIT 1), rand2.model_id
    FROM tbl_random as rand2

Then you will have to run a second query to update the more_data using make and model as the unique key. 然后,您将不得不运行第二个查询以使用make和model作为唯一键来更新more_data。

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

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