Mysql 根据从另一个表中选择来更新所有行

发布于 2024-10-14 10:44:08 字数 1745 浏览 1 评论 0原文

我有两张桌子;

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。我该怎么做?

谢谢!

I have two tabels;

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                |                |
+----------------------+------------+------+-----+---------------------+----------------+

I can select country code using ip address from first table:

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           |
+--------------+

In logs.logs, I have all the REMOTE_ADDR (ip address) set, but all COUNTRY_CODE entries are empty. Now, I want to populate COUNTRY_CODE appropriately using the ipinfo table. How can I do this?

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感情废物 2024-10-21 10:44:08

尝试

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))。

Try

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

If it fails saying the column types must match, you'll have to alter your logs.logs table so that the REMOTE_ADDR column is the same type (varchar(20)) as the ip_cidr table.

拿命拼未来 2024-10-21 10:44:08

在单表更新中,您可以使用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 messages.logs as l, ipinfo.ip_group_country as c set l.COUNTRY_CODE=c.country_code where c.ip_start <= INET_ATON(l.REMOTE_ADDR) order by c.ip_start asc

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

In a single-table update you use update t1 set c1=x where y.

In a multi-table update you use update t1, t2 set t1.c1=t2.c2 where t1.c3=t2.c4

Here's the relevant documentation http://dev.mysql.com/doc/refman/5.0/en/update.html

What you're looking for is something along the lines of (editted) update logs.logs as l, ipinfo.ip_group_country as c set l.COUNTRY_CODE=c.country_code where c.ip_start <= INET_ATON(l.REMOTE_ADDR) order by c.ip_start asc

Edit: you're right, the max() in the original answer I provided could not work. The query above should, although it will likely be less efficient than something like the approach in the answer provided below.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文