MySQL turns a JOIN statement into a stored procedure or function?
So I have a JOIN operator that I will be using in several places. It essentially finds the IP address and matches it to the specified location from the staging table. I need to pass in two variables - one of which is my database / schema prefix and the other is the IP address itself.
So a function is used CONCAT()
to put it together.
So at the moment I have a procedure that looks something like this:
CREATE DEFINER=`root`@`%` PROCEDURE `LocationFromIp`(
ipAddress VARCHAR(16),
clientComp VARCHAR(32)
)
BEGIN
DECLARE test VARCHAR(255);
SET @networkSql = CONCAT("
SET @Location =
(SELECT `network`.`Name`
FROM `", clientComp, "-settings`.`iptable` AS `iptable`
LEFT JOIN `", clientComp, "-settings`.`network` AS `network`
ON `network`.`Subnet` = `iptable`.`Subnet`
WHERE `iptable`.`IP` = '", ipAddress, "'
LIMIT 1);
");
PREPARE test1 FROM @networkSql;
EXECUTE test1;
SELECT @Location AS `Location`;
It returns the result I want, but I don't know how to use it in the statement.
SELECT `IPAddress` AS CALL LocationFromIp('clientComp', `IPAddress`)
(
SELECT `IPAddress`
FROM `clientComp-data`.`tablename`
WHERE @date > `Date`;
)
GROUP BY `IPAddress`
The above doesn't work, but I hope you can understand my thinking!
So how would I go about it?
+3
source to share