【问题标题】:Mysql update all rows based on select from another tableMysql根据从另一个表中选择更新所有行
【发布时间】:2023-03-22 19:26:01
【问题描述】:

我有两个表;

mysql> describe ipinfo.ip_group_country;
+--------------+-------------+------+-----+---------+-------+
| Field        | Type        | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| ip_start     | bigint(20)  | NO   | PRI | NULL    |       |
| ip_cidr      | varchar(20) | NO   |     | NULL    |       |
| country_code | varchar(2)  | NO   | MUL | NULL    |       |
| country_name | varchar(64) | NO   |     | NULL    |       |
+--------------+-------------+------+-----+---------+-------+

mysql> describe logs.logs;
+----------------------+------------+------+-----+---------------------+----------------+
| Field                | Type       | Null | Key | Default             | Extra          |
+----------------------+------------+------+-----+---------------------+----------------+
| id                   | int(11)    | NO   | PRI | NULL                | auto_increment |
| ts                   | timestamp  | NO   |     | CURRENT_TIMESTAMP   |                |
| REMOTE_ADDR          | tinytext   | NO   |     | NULL                |                |
| COUNTRY_CODE         | char(2)    | NO   |     | NULL                |                |
+----------------------+------------+------+-----+---------------------+----------------+

我可以使用第一个表中的 IP 地址选择国家代码:

mysql> SELECT country_code FROM ipinfo.`ip_group_country` where `ip_start` <= INET_ATON('74.125.45.100') order by ip_start desc limit 1;
+--------------+
| country_code |
+--------------+
| US           |
+--------------+

在 logs.logs 中,我设置了所有 REMOTE_ADDR(IP 地址),但所有 COUNTRY_CODE 条目都是空的。现在,我想使用 ipinfo 表适当地填充 COUNTRY_CODE。我该怎么做?

谢谢!

【问题讨论】:

    标签: mysql select


    【解决方案1】:

    试试

    UPDATE logs.logs
    SET COUNTRY_CODE = (
        SELECT country_code
        FROM ipinfo.ip_group_country
        WHERE ipinfo.ip_start <= INET_ATON(logs.REMOTE_ADDR)
        LIMIT 1
    )
    WHERE COUNTRY_CODE IS NULL
    

    如果无法说明列类型必须匹配,则必须更改 logs.logs 表,使 REMOTE_ADDR 列与 ip_cidr 表的类型相同 (varchar(20))。

    【讨论】:

      【解决方案2】:

      在单表更新中,您使用update t1 set c1=x where y

      在多表更新中,您使用update t1, t2 set t1.c1=t2.c2 where t1.c3=t2.c4

      这里是相关文档http://dev.mysql.com/doc/refman/5.0/en/update.html

      您要查找的内容类似于(已编辑)update logs.logs as l, ipinfo.ip_group_country as c set l.COUNTRY_CODE=c.country_code where c.ip_start &lt;= INET_ATON(l.REMOTE_ADDR) order by c.ip_start asc

      编辑:你说得对,我提供的原始答案中的 max() 不起作用。上面的查询应该,尽管它可能比下面提供的答案中的方法效率低。

      【讨论】:

      • 谢谢。我收到一个错误:mysql&gt; update logs.logs as l, ipinfo.ip_group_country as c set l.COUNTRY_CODE=max(c.country_code) where c.ip_start &lt;= INET_ATON(l.REMOTE_ADDR); ERROR 1111 (HY000): Invalid use of group function
      • 抱歉,你说得对,我已经用应该更好的东西对其进行了编辑。
      猜你喜欢
      • 2012-03-23
      • 2011-03-30
      • 2017-02-11
      • 2021-11-05
      • 1970-01-01
      • 2013-06-04
      • 1970-01-01
      • 1970-01-01
      • 2013-05-25
      相关资源
      最近更新 更多