我使用MY SQL创建了一个程序,其中有9个查询.3个条件如果满足任何条件,则执行每个条件的3个查询。 看起来没有显示语法错误。有四个IN参数和三个OUT参数
CREATE DEFINER=`root`@`localhost` PROCEDURE `count_student_by_uni`(IN `uni_id` INT(10), IN `campus_id` INT(10), IN `dept_id` INT(10), OUT `approved` INT(10), OUT `disapproved` INT(10), OUT `applied` INT(10), IN `city_id` INT(10))
NO SQL
Begin
SET approved=0;
SET disapproved=0;
SET applied=0;
if( (uni_id is NOT NULL)
AND
(campus_id is NOT NULL )
AND
(dept_id is NOT NULL ) AND (city_id is Null) ) then
/* Approved student based on uni,camp and dept */
SELECT count(*) into approved from student s join scheme_data sd on s.id=sd.s_id where sd.isdata_locked=0 AND s.is_verified=1 AND sd.university_id=uni_id AND sd.campus_id=campus_id AND sd.school_dept_id=dept_id AND sd.studentstatus_id=11;
/* Disapproved student based on uni,camp and dept */
SELECT count(*) into disapproved from student s join scheme_data sd on s.id=sd.s_id where sd.isdata_locked=0 AND s.is_verified=0 AND sd.university_id=uni_id AND sd.campus_id=campus_id AND sd.school_dept_id=campus_id AND sd.studentstatus_id=9;
/* Applied student based on uni,camp and dept */
SELECT count(*) into applied from student s join scheme_data sd on s.id=sd.s_id where sd.isdata_locked=0 AND s.is_verified is null AND sd.university_id=uni_id AND sd.campus_id=campus_id AND sd.school_dept_id=dept_id AND sd.studentstatus_id=9;
END IF;
if (uni_id is NOT NULL) then
/* Approved student based on uni */
SELECT count(s.id) into approved from student s join scheme_data sd on s.id=sd.s_id where sd.isdata_locked=0 AND s.is_verified=1 AND sd.university_id=uni_id AND sd.studentstatus_id=11;
/* Disapproved student based on uni */
SELECT count(s.id) into disapproved from student s join scheme_data sd on s.id=sd.s_id where sd.isdata_locked=0 AND s.is_verified=0 AND sd.university_id=uni_id AND sd.studentstatus_id=9;
/* Applied student based on uni */
SELECT count(s.id) into applied from student s join scheme_data sd on s.id=sd.s_id where sd.isdata_locked=0 AND sd.university_id=uni_id AND sd.studentstatus_id=9 AND s.is_verified is NULL;
END IF;
if (city_id is NOT NULL) then
/* Approved student based on city */
SELECT count(s.id) into approved from student s join scheme_data sd on s.id=sd.s_id where sd.isdata_locked=0 AND s.is_verified=1 AND sd.city_id=city_id AND sd.studentstatus_id=11;
/* Disapproved student based on city */
SELECT count(s.id) into disapproved from student s join scheme_data sd on s.id=sd.s_id where sd.isdata_locked=0 AND s.is_verified=0 AND sd.city_id=city_id AND sd.studentstatus_id=9;
/* Applied student based on city */
SELECT count(s.id) into applied from student s join scheme_data sd on s.id=sd.s_id where sd.isdata_locked=0 AND sd.city_id=city_id AND sd.studentstatus_id=9 AND s.is_verified is NULL;
END IF;
end
并且调用程序是这样的:
当PARAM为$ uni_id时,$ campus_id和$ dept_id
$r=mysql_query("CALL `count_student_by_uni`($allowed_university, $allowed_campus,$allowed_depts,@p3,@p4,@p5)");
$q = mysql_query(" SELECT @p3 AS `approved`, @p4 AS `disapproved`,@p5 AS `applied`")or die(mysql_error());
其中$ allowed_university,$ allowed_campus和$ allowed_depts =任何整数值
当PARAM为$ uni_id时
r=mysql_query("CALL `count_student_by_uni`($allowed_university, @p1='',@p2='',@p3='',@p4='',@p5='')");
$q = mysql_query("SELECT @p3 AS `approved`, @p4 AS `disapproved`, @p5 AS `applied`" ) or die(mysql_error());
其中$ allowed_university =任何整数值
在PARAM中是city_id
$r=mysql_query("CALL `count_student_by_uni`(@p0, @p1,@p2,@p3,@p4,@p5,$city)");
$q = mysql_query(" SELECT @p3 AS `approved`, @p4 AS `disapproved`,@p5 AS `applied`")or die(mysql_error());
其中$ city =任何整数值 只有最后一个条件是工作,其他两个是不工作的善意帮助??