Postgres Cast macaddr

Suppose I have two tables in Postgres:

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

Name: table_mac
Column    Type
id        integer
mac       macaddr

I want to make a connection:

SELECT * FROM table_rad WHERE username = mac;

Postgres will complain:

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.

So far. I found a solution to solve the problem, and I know that I need CAST. But how can I impose a macaddr type like varhcar?

+3
source share
2 answers

Formal style: CAST(mac AS varchar)

PostgreSQL style: mac::varchar

eg:.

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

You can do this using ::textas below:

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

Source: https://habr.com/ru/post/1796013/


All Articles