Postgres CAST macaddr

时间:2011-03-03 11:04:28

标签: postgresql casting

假设我在Postgres有两张桌子:

Name: table_rad
Column    Type   
id        integer
username  character varying(64)

Name: table_mac
Column    Type
id        integer
mac       macaddr

我想加入:

SELECT * FROM table_rad WHERE username = mac;

Postgres会抱怨:

ERROR: operator does no exist: character varying = macaddr
LINE 1: ...ELECT * from table_rad WHERE username = mac;
                                                 ^    
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.

Sofar我已经google了解决方案,我知道我必须CAST。但是如何将类型macaddr转换为varhcar?

2 个答案:

答案 0 :(得分:2)

正式风格:CAST(mac AS varchar)

PostgreSQL风格:mac::varchar

e.g:

SELECT * FROM table_rad JOIN table_mac ON username = CAST(mac AS varchar)

答案 1 :(得分:0)

您可以通过以下::text执行此操作:

SELECT * FROM table_rad WHERE username = mac::text;