RLIKE uses Java regular expressions. So you can use "^" to express starting with something. For example, you can use 'CONCAT("^",client.ip)' to put the "^" before client.ip
.
SELECT client.name, count(access.ip)
FROM access JOIN client
WHERE access.ip RLIKE CONCAT("^",client.ip)
GROUP BY client.name;
However, since "." is also a special character in a regular expression that means any character. So the above solution is not perfect. For example, if the client ip is 1.3
, it may match '103.2.3.4'. So a better solution is escaping the "." in the client ip. Here is the final solution:
SELECT client.name, count(access.ip)
FROM access JOIN client
WHERE access.ip RLIKE CONCAT("^",REGEXP_REPLACE(client.ip, "\\.", "\\."))
GROUP BY client.name;
The first \\.
means a regular expression \.
(We need to add "\" to specify "\" in Hive). The second \\.
means a string \.
. If you are not familiar with Java regular expression, it may confuse you.