我的数据库中有两个表,一个是tablea
,另一个是tableb
。
SELECT *
FROM TABLEA
结果:
╔═════════════════════════════════════════════════════╗
║ ip mac ║
╠═════════════════════════════════════════════════════╣
║ 1.10.0.0.97 00 14 2A 2F 72 FE ║
║ 1.10.0.0.98 08 CC 68 71 A1 C0 ║
╚═════════════════════════════════════════════════════╝
SELECT *
FROM TABLEB
结果:
╔═══════════════════════════════════════╗
║ mac port ║
╠═══════════════════════════════════════╣
║ 0:14:2a:2f:72:fe 24 ║
║ 8:cc:68:71:a1:c0 7 ║
╚═══════════════════════════════════════╝
我现在想要创建第三个表,它连接tablea和表c,并有3列显示ip,mac和port。该表已经创建,这就是我的想法;
INSERT INTO TABLEC
SELECT A.IP,
A.MAC,
B.PORT
FROM TABLEA A,
TABLEB B
WHERE A.MAC = REPLACE('REPLACE('B.MAC',':',' ')','0','00')
查询给我一个错误
ERROR 1064(42000): You have an error in your SQL syntax; check the manual that corresponds to your mysql server version for the right syntax to use near 'b.mac',':',' ')','0','00')
我检查了手册,但我无法确定错误的位置。而且我的替换功能也可以将0:14:2a:2f:72:fe转换为00 14 2A 2F 72 FE但是如果尝试8:cc:68:71:a1:c0它将无效。我真的很感激我在创建第3张桌子时可以获得的任何帮助
答案 0 :(得分:2)
您应该在'
之前和replace
之后移除)
以及b.mac
周围的引号。
另外,您无法将0
替换为00
,因为对于c0
,它将返回c00
,您应该使用 LPAD 。< / p>
INSERT INTO TABLEC
SELECT A.IP,
A.MAC,
B.PORT
FROM TABLEA A,
TABLEB B
WHERE A.MAC = UPPER(LPAD(REPLACE(B.MAC, ':', ' '), 17, '0'))
答案 1 :(得分:2)
在进行替换之前,你应该使用lpad(带0)tableb.mac。
答案 2 :(得分:2)
INSERT INTO tablec
SELECT a.ip, a.mac, b.port
FROM tablea a
LEFT JOIN tableb b
ON a.mac = upper(LPAD(replace(b.mac,':',' '),17,'0'))
parado's replace比我写的更有意义 -