I trying to write MySql Function to return the count of child for one node and the I wrote this code
CREATE DEFINER=`root`#`localhost` FUNCTION `get_child_count`(`id` INT) RETURNS int(11)
READS SQL DATA
Begin
declare temp int(11);
SELECT count(*) into temp FROM tbl_photo_album where parent_id = id;
return temp;
End
and everytime I tried to run this function it come with nothing.
Possible alternative: Instad of select ... into..., try this:
set temp = (select count(*) from tbl_photo_album where parent_id = id);
try this one.
DELIMITER $$
CREATE DEFINER=`root`#`localhost` FUNCTION `get_child_count`(`id` INT) RETURNS int(11)
READS SQL DATA
Begin
declare temp int(11);
SELECT count(*) into temp FROM tbl_photo_album where parent_id = id;
return temp;
END$$
DELIMITER ;
select get_child_count(10); -- Pass Id for what you want result.
Related
I make a this stored function to return the amount of invoices per customer:
delimiter //
CREATE FUNCTION function1(id INT) RETURNS INT READS SQL DATA
BEGIN
DECLARE result INT;
(SELECT count(invoice_id) INTO #result FROM invoices WHERE customer_id = #id);
RETURN #result;
END//
delimiter ;
but when I use it, returns 0:
SELECT function1(12) AS Q;
and the query returns 428 :
SELECT count(invoice_id) AS Q FROM invoices WHERE customer_id = 12;
I need to know what am I doing wrong.
#id is not the same as id
But it id better to use variable names _id to differentiate variables from column names
delimiter //
CREATE FUNCTION function1(_id INT) RETURNS INT READS SQL DATA
BEGIN
DECLARE result INT;
(SELECT count(invoice_id) INTO #result FROM invoices WHERE customer_id = _id);
RETURN #result;
END//
delimiter ;
trying to get the total number of boxes ordered from a given type("mytype").
something seems to be wrong with my syntax
create function NumOrdersForBoxType (mytype varchar(20))
returns int
begin
DECLARE #numorders int
select #numOrders = count(*)
from BOXES as B
where B.Type = mytype
return #numOrders
end
;
In mysql you don't declare user defined variables (at variables) stackoverflow.com/questions/11754781/… AND every statement needs to be terminated and since you have more than 1 statement in the function you need to wrap in begin..end and possibly set delimiters. AND you need to SET variables
delimiter $$
create function f(mytype varchar(20))
returns int
begin
DECLARE numorders int;
set numorders = (select count(*)
from boxes as B
where B.Type = mytype
);
return numOrders;
end $$
delimiter ;
I have the next stored procedure:
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `getUserIdByLogin`(userId VARCHAR(255))
BEGIN
SELECT id FROM `userdata` WHERE login = userId;
END
I want to declare a new variable #tmp for e.g. and do smth to this:
SET #tmpValue = CALL getUserIdByLogin("someLogin");
But it doesn't work.
If just to call:
CALL getUserIdByLogin("someLogin");
Then I would see results, but I need to declare the results in the variable ( of array type ).
How can I do it?
Thanks!
CREATE DEFINER=`root`#`localhost` PROCEDURE `getUserIdByLogin`(
userId VARCHAR(255),
OUT idout int
)
BEGIN
SELECT id INTO idout FROM `userdata` WHERE login = userId;
END
then
SET #id = 0;
CALL getUserIdByLogin("someLogin", #id);
SELECT #id;
I have a stored function in MySQL and it works partially.
DELIMITER $$
DROP FUNCTION IF EXISTS `getsubdomain`$$
CREATE FUNCTION getsubdomain(page_id int(11))
RETURNS CHAR(255)
DETERMINISTIC
READS SQL DATA
SQL SECURITY INVOKER
BEGIN
declare current_p_id int(11);
declare current_p_parent_id int(11);
declare current_p_address_type char(255);
declare current_p_adress char(255);
SET current_p_id = page_id;
WHILE (current_p_id) <> 0
DO
select p_parent_id, p_address_type, p_adress from opu_pages where p_id = current_p_id into current_p_parent_id, current_p_address_type, current_p_adress;
IF current_p_address_type <> ''
THEN
IF current_p_address_type = 'subdomain'
THEN
RETURN current_p_adress;
ELSE
SET current_p_id = current_p_parent_id;
END IF;
ELSE
RETURN NULL;
END IF;
END WHILE;
RETURN NULL;
END$$
DELIMITER ;
If I call in query SELECT getsubdomain(p_id) FROM opu_pages; it works Ok. But if I call it in SELECT * FROM opu_pages WHERE getsubdomain(p_id)='library'; the database is collapsed and freezing.
Query and function work with one table.
What did I do wrong?
I thought that it can be caused by the table format MyISAM. But I can't change it to InnoDB because I use FULLTEXTFORMAT fields in this table.
Table opu_pages (MyISAM) scheme
p_id INT
p_parent_id INT
p_address_type ENUM (path, subdomain)
p_adress VARCHAR
Based on your post I would say that your code is entering an infinite loop for some of your input parameters.
In particular the case where p_id = p_parent_id in the opu_pages table and the current_p_address_type = 'subdomain'
I am running a stored procedure. The issue seems to be that it will go into the if statement. Also for some reason or another regardless of how many selects I use it will only return the first. I've copied this from another stored procedure that works like a charm, but this one just won't go. Any ideas?
DROP PROCEDURE IF EXISTS genSelPriceTier;
DELIMITER $$
CREATE PROCEDURE genSelPriceTier(tier_id INT, default_id INT)
BEGIN
DECLARE rowCount INT DEFAULT 0;
SELECT * FROM price_tier WHERE price_tier_id = tier_id;
SET rowCount = FOUND_ROWS();
IF rowCount < 1 THEN
SELECT * FROM price_tier WHERE price_tier_id = default_id;
END IF;
END$$
DELIMITER ;
There is a bug reported related to the usage of FOUND_ROWS(). So, I recommend using Count(*) for the number of rows returned. Something like the following should work.
DROP PROCEDURE IF EXISTS genSelPriceTier;
DELIMITER $$
CREATE PROCEDURE genSelPriceTier(tier_id INT, default_id INT)
BEGIN
DECLARE rowCount INT DEFAULT 0;
SELECT COUNT(*) INTO rowCount FROM price_tier WHERE price_tier_id = tier_id
IF rowCount < 1 THEN
SELECT * FROM price_tier WHERE price_tier_id = default_id;
END IF;
END$$
DELIMITER ;