Mysql基于从另一个表中选择更新所有行
我有两个标签Mysql基于从另一个表中选择更新所有行,mysql,select,Mysql,Select,我有两个标签 mysql> describe ipinfo.ip_group_country; +--------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+-------------+------+-----+---------+-------+ | ip_start
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中,我设置了所有远程地址ip地址,但所有国家/地区代码条目都为空。现在,我想使用ipinfo表适当地填充COUNTRY_代码。我该怎么做
谢谢 在单个表更新中,使用update t1 set c1=x,其中y 在多表更新中,使用updatet1,t2 set t1.c1=t2.c2,其中t1.c3=t2.c4 这是相关的文件 您要查找的是经过编辑的更新日志。日志为l,ipinfo.ip\u group\u country为c set l.country\u CODE=c.country\u CODE,其中c.ip\u开始Try
如果说列类型必须匹配失败,您必须更改logs.logs表,以便远程添加列的类型与ip\u cidr表的类型相同。谢谢。我得到一个错误:mysql>将logs.logs更新为l,ipinfo.ip\u group\u country为c set l.country\u CODE=maxc.country\u c.ip\u开始的地方很抱歉,你是对的,我已经用一些应该更好的东西编辑了它。
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