简体   繁体   English

具有多个记录的MySQL CREATE FUNCTION结果给出错误#1172

[英]MySQL CREATE FUNCTION result with multiple records gives error #1172

I have the following 3 tables: 我有以下3表:

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. 现在,当我运行SELECT get_word('pen')时,它返回黄色 ,这是预期的结果。

but when I run the code SELECT get_word('book') it get error: #1172 - Result consisted of more than one row 但是,当我运行代码SELECT get_word('book')时 ,出现错误:# 1172-结果包含多于一行

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

Well, it seems that you want a resultset (more than one value) 好吧,似乎您想要一个结果集(多个值)

But a mysql stored function cannot return a resultset (see doc : Restrictions for Stored Functions). 但是mysql存储函数不能返回结果集(请参阅doc :存储函数的限制)。

Solution 1 : use a stored procedure 解决方案1 :使用存储过程

CREATE PROCEDURE get_word(n VARCHAR(20))
BEGIN
 SELECT  `color`.`color` 
 FROM   `name` 
   LEFT JOIN `both` 
          ON `name`.`id`=`both`.`name`
   LEFT JOIN `color`
          ON `color`.`id`=`both`.`color` 
   WHERE `name`.`name`=n; 
END

Solution2 : return a concatenated string 解决方案2 :返回一个串联的字符串

something like "green, yellow, blue" GROUP_CONCAT can do that easily. 诸如"green, yellow, blue" GROUP_CONCAT之类的内容可以轻松实现。 see this stackoverflow question for example 例如,请参阅此stackoverflow问题

尝试在where之后使用group by,例如,尝试group by id或表中唯一的内容添加group by id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 结果包含多行Error 1172 mysql - Result consisted of more than one row Error 1172 mysql 错误 1172 - 结果在 MySQL 中包含多于一行 - Error 1172 - Result consisted of more than one row in MySQL MySQL 错误 1172 - 结果包含多于一行 - MySQL Error 1172 - Result consisted of more than one row MySQL存储过程在不使用游标的情况下遍历多个值,返回错误代码:1172。结果包含多于一行 - MySQL stored procedure iterate through multiple values with no cursor used return Error Code: 1172. Result consisted of more than one row mysql存储函数-在varchar中并返回varchar-错误:1172 - mysql stored function - in varchar and return varchar - Error : 1172 MySql存储过程-错误1172(42000):结果包含多于一行 - MySql Stored Procedure - ERROR 1172 (42000): Result consisted of more than one row 错误 1172 (42000):结果在 mysql 存储过程中包含多于一行 - ERROR 1172 (42000): Result consisted of more than one row on mysql stored procedure 错误代码:1172。结果由 mysql 中的多行组成 - Error Code: 1172. Result consisted of more than one row in mysql 错误代码:1172。结果包含多行[MySQL] - Error Code: 1172. Result consisted of more than one row [MySQL] mysql 存储过程错误(1172,'Result consisted of more than one row') - mysql stored procedure error (1172, 'Result consisted of more than one row')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM