虽然它运作正常,我想学习(我在网上阅读并查找示例但找不到任何内容)如果我在下面要求可能。正如你在我的函数中看到的那样,我将3列连接起来作为一个单独的字符串返回并在我的PHP代码中将其展开以使用它们。
我可以在不使用CONCAT_WS()
的情况下一次性单独返回这3列,而不是在PHP中使用explode
吗?
由于
我的MYSQL功能:
DROP FUNCTION IF EXISTS function_institute_university;
DELIMITER $$
CREATE FUNCTION function_institute_university (IN_partnership_id INTEGER(11))
RETURNS VARCHAR(250) DETERMINISTIC
BEGIN
DECLARE InsCode_UniID_UniCode VARCHAR(250);
SELECT
CONCAT_WS('~',
UPPER(institute.Code),
university.id,
UPPER(university.UnivAcronym)
) INTO InsCode_UniID_UniCode
FROM partnership
LEFT JOIN institute ON institute.id = partnership.iid
LEFT JOIN university ON university.id = partnership.uid
WHERE
partnership.id = IN_partnership_id AND
partnership.active = '1'
LIMIT 1;
RETURN InsCode_UniID_UniCode;
END$$
DELIMITER ;
我的PHP代码:
$sql = "SELECT function_institute_university(15)";
$runSql = mysql_query($sql);
if(@mysql_num_rows($runSql) == 1)
{
$newArray = mysql_fetch_array($runSql);
list($instituteCode, $universityID, $universityCode) = explode('~', $newArray[0]);
}
答案 0 :(得分:2)
更新:第二次看后,我发现您正在使用一个只返回一个值的函数(完全被忽略),您可以将其更改为过程吗?
SQL:
DROP PROCEDURE IF EXISTS procedure_institute_university;
DELIMITER $$
CREATE PROCEDURE procedure_institute_university (IN_partnership_id INTEGER(11))
BEGIN
SELECT
UPPER(institute.Code) as `institiute_code`,
university.id as `university_id`,
UPPER(university.UnivAcronym) as `university_acronym`
FROM partnership
LEFT JOIN institute ON institute.id = partnership.iid
LEFT JOIN university ON university.id = partnership.uid
WHERE
partnership.id = IN_partnership_id AND
partnership.active = '1'
LIMIT 1;
END$$
PHP:
$sql = "call procedure_institute_university(15)";
$runSql = mysql_query($sql);
if(@mysql_num_rows($runSql) == 1)
{
$newArray = mysql_fetch_array($runSql);
list($instituteCode, $universityID, $universityCode) = $newArray[0]['institute_code'], $newArray[0]['university_id'], $newArray[0]['university_acronym'];
}
添加了别名,以便您可以确定检索到的内容。您也可以使用mysql_fetch_assoc
(http://php.net/manual/en/function.mysql-fetch-assoc.php)来获取关联数组。阅读文档时,请阅读该页面中的警告信息。