I have the following 3 tables:
table "name" --------------- id name --------------- 1 book 2 pen table "color" ------------------ id color ------------------ 1 red 2 yello 3 green 4 pink table "both" ------------------------ id name color ---------------------- 1 1 1 2 1 2 3 1 3 4 2 2
and I have the following function:
DELIMITER //
CREATE FUNCTION get_word(n VARCHAR(20))
RETURNS VARCHAR(10)
READS SQL DATA
DETERMINISTIC
BEGIN
DECLARE b VARCHAR(20);
SELECT `color`.`color` INTO b FROM `name`
LEFT JOIN `both`
ON `name`.`id`=`both`.`name`
LEFT JOIN `color`
ON `color`.`id`=`both`.`color`
WHERE `name`.`name`=n;
RETURN b;
END//
DELIMITER ;
now when I run SELECT get_word('pen') it returns yellow which is what is expect.
but when I run the code SELECT get_word('book') it get error: #1172 - Result consisted of more than one row
my Question: What to do so this function works with multiple records as well as single record which it does when I search for "pen"? thanks
UPDATE:
If use the query without the function as follow, it just works fine:
SELECT `color`.`color` AS b FROM `name`
LEFT JOIN `both`
ON `name`.`id`=`both`.`name`
LEFT JOIN `color`
ON `color`.`id`=`both`.`color`
WHERE `name`.`name`='book';
and it returns:
**b** red yellow green